targeting parrot

24
Targeting Parrot Léon Brocard Fotango Ltd. [email protected]

Upload: berke

Post on 08-Jan-2016

72 views

Category:

Documents


2 download

DESCRIPTION

Targeting Parrot. Léon Brocard Fotango Ltd. [email protected]. Outline. What is Parrot? Why target Parrot? Targeting Modifying Parrot. Targeting?. set I0, 24 set I2, 1 set I3, 1 set I4, 1 set I5, 3 LBL1: gt I5, I0, LBL2 print I5 print "\n" add I4, I2, I3 - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Targeting Parrot

Targeting ParrotTargeting Parrot

Léon BrocardFotango Ltd.

[email protected]

Page 2: Targeting Parrot

OutlineOutline

• What is Parrot?• Why target Parrot?• Targeting• Modifying Parrot

Page 3: Targeting Parrot

Targeting?Targeting?

public class Fib { static void Main() { int n = 24, a = 1, b = 1, f = 1, i = 3;

while(i <= n) { puti(i); puts("\n"); f = a + b; a = b; b = f; i++; } }}

set I0, 24 set I2, 1 set I3, 1 set I4, 1 set I5, 3LBL1: gt I5, I0, LBL2 print I5 print "\n" add I4, I2, I3 set I2, I3 set I3, I4 set I1, I5 inc I5 branch LBL1LBL2: end

Page 4: Targeting Parrot

Parrot is…Parrot is…

• Virtual machine for dynamic languages

• Register-based (I, N, S, P)• Portable (bytecode-based)• High-level• Fast• Moving target

Page 5: Targeting Parrot

Targeting consists of:Targeting consists of:

• Parsing the source language• (Doing some data structure

munging)• Outputting assembler / machine

code

Page 6: Targeting Parrot

Java Virtual MachineJava Virtual Machine

• Two stage (javac, java)

int x, y, z;x = 1;y = 1;z = x + y;

iconst_1istore_1iconst_1istore_2iload_1iload_2iaddistore_3

Page 7: Targeting Parrot

TargetingTargeting

• Expression by expression• Parrot does not have for, while

• Use branch instead

Page 8: Targeting Parrot

Parrot Virtual MachineParrot Virtual Machine

• Two stage (assemble.pl, parrot)

0000000 0004 0000 0000 4018 0080 0000 0010 00000000020 55a1 0131 524c 5045 0000 0000 002c 00000000040 0001 0000 0073 0000 0020 0000 0000 00000000060 0000 0000 0001 0000 000d 0000 6548 6c6c0000100 206f 6f77 6c72 2164 000a 0000 000c 00000000120 0018 0000 0000 0000 0000 00000000134

print “Hello world!\n”end

Page 9: Targeting Parrot

Parrot Virtual Machine (2)Parrot Virtual Machine (2)

• Two stage (assemble.pl, parrot)

% ./assemble.pl hello.pasm -o hello.pbc% ./parrot hello.pbcHello world!%

Page 10: Targeting Parrot

Main methods of targeting

Main methods of targeting

• Output Parrot source code (.pasm)• Output Parrot bytecode

• Output interpreter (BASIC)• Output code (cola, jako)• Hybrid

Page 11: Targeting Parrot

Parrot is dynamicParrot is dynamic

• Not only for dynamic languages, but also fairly dynamic itself

• Modify Parrot for your own use:• Modify the virtual machine• Modify the opcodes• Modify the PMCs

Page 12: Targeting Parrot

TargetingTargeting

• Want to run your language under Parrot?

• Got an existing parser? Simply retarget your language at Parrot

Page 13: Targeting Parrot

Parrot is a register machine

Parrot is a register machine

• Mostly for speed:add I0, I1, I2

• Also a stack machine, so if targeting a stack language, use the stack for ease of translation:

restore I1restore I2add I0, I1, I2save I0

Page 14: Targeting Parrot

Adding opsAdding ops

• What was that about a stack machine?

op iadd() { INTVAL x; INTVAL y; stack_pop(interpreter, interpreter->user_stack, &x, STACK_ENTRY_INT); stack_pop(interpreter, interpreter->user_stack, &y, STACK_ENTRY_INT); x = x + y; stack_push(interpreter, interpreter->user_stack, &x, STACK_ENTRY_INT, STACK_CLEANUP_NULL); goto NEXT();}

Page 15: Targeting Parrot

Register allocationRegister allocation

• Use registers for speed• Use graph colouring, du-chains for

lexicals and symbolic temporaries• Register spilling: use spill weights• “Compilers: Principles, Techniques

and Tools” by Alfred V. Aho, Ravi Sethi, and Jeffrey D. Ullman

Page 16: Targeting Parrot

Register allocation: imccRegister allocation: imcc

• imcc: intermediate compiler for Parrot by Melvin Smith and Angel Faus

• handles register allocation and spillage (infinite number of typed temporaries or named lexicals)

• in the future: constant folding, expression evaluation, instruction selection, other optimisations…

Page 17: Targeting Parrot

Register allocation: imccRegister allocation: imcc

_MAIN: save "" set I0, 4 add I0, I0, 1 set I0, I0 print I0 print "\n" end ret

.sub _MAIN .arg "" .local int out $I0 = 4 $I1 = $I0 + 1 out = $I1 print out print "\n" end ret

Page 18: Targeting Parrot

Adding PMCsAdding PMCs

• A little more involved (docs/vtables.pod)

• SchemePair PMC• car, cdr and all that jazz

• Add ops which use the PMCs• Caveat: tricky, underdocumented

atm.

Page 19: Targeting Parrot

Calling ConventionsCalling Conventions

• PDD03: Parrot Calling Conventions• Caller-save• Important for exposing public

subroutines• Mostly ignoreable

Page 20: Targeting Parrot

Garbage collectionGarbage collection

• Parrot has a GC system

Page 21: Targeting Parrot

DebuggingDebugging

• Parrot debugger• pdb

• Trace with the -t flag• … or print statements

Page 22: Targeting Parrot

TODO ;-)TODO ;-)

• Wait for Parrot to have more features, target some more languages at it and then finish these slides

• Ship parrot with your language?• Type conversions / stack

manipulations / PMC / control / subroutines / exceptions /

Page 23: Targeting Parrot

If you remember one thing…

If you remember one thing…

• Computer languages can easily be targeted at Parrot

Page 24: Targeting Parrot

Thank youThank you