Skip to content

Latest commit

 

History

History
769 lines (490 loc) · 16.5 KB

compiler_faq.pod

File metadata and controls

769 lines (490 loc) · 16.5 KB

NAME

docs/compiler_faq.pod - Parrot FAQ for compiler writers in PIR

General Questions

Which C compilers can I use with Parrot?

Whoa, there--you're looking at the wrong FAQ. This document is for people writing compilers that target Parrot.

To answer your question, though, Parrot should theoretically work with any C89-compliant C compiler. See the README files in the root directory for more information about building Parrot.

How can I implement a compiler to use as a compiler object from within Parrot?

See http://www.parrotblog.org/2008/03/targeting-parrot-vm.html.

How do I embed source locations in my code for debugging?

Use .line 42 "file.pir" for this.

Subroutines

How do I generate a sub call in PIR?

This looks like a function call in many HLLs:

where $P0 is the function object, and $P1, $P2, and $P3 are its parameters. You can also use a function's name in place of the object, as long as it's in the same namespace.

You can also get return value(s):

If the function name might collide with a Parrot opcode, quote it:

You can also use the full PCC for these calls. See "pdd19_pir.pod/Parameter Passing and Getting Flags" in docs and other questions below for more information.

How do I generate a method call in PIR?

Similar to function calls, just append . and the method name to the object. You should quote a literal method name to avoid confusion.

The method name may also be a string variable representing a method name:

How do I locate or create a subroutine object?

There are several ways to achieve this, depending on the location of the subroutine.

If the sub is in the same file use a Sub constant:

A more dynamic way is:

This searches for a subroutine 'foo' in the current lexical pad, in the current namespace, in the global, and in the builtin namespace in that order. This opcode is generated, if foo() is used, but the compiler can't figure out, where the function is.

If the subroutine is in a different namespace, use the get_hll_global or get_root_global opcodes:

This fetches the sub foo in the Foo namespace.

How do I create a Closure or Coroutine?

Closure and Coroutine carry both a dynamic state. Therefore you need to perform two steps. First use one of the above ways to locate the Sub object. Then use the op newclosure to capture the environment.

Any subroutine that contains a .yield directive is automatically created as a Coroutine PMC:

How do I generate a tail call in PIR?

The sub bar will return to the caller of foo. (Warning! This fails in some cases. XXX Find the Trac ticket and reference it here.)

How do I generate a sub call with a variable-length parameter list in PIR?

If you have a variable amounts of arguments in an array, you can pass all items of that array with the :flat directive.

How to I retrieve the contents of a variable-length parameter list being passed to me?

Use a slurpy array:

If you have a few fixed parameters too, you can use a slurpy array to get the rest of the arguments

How do I pass optional arguments?

Use the :optional and :opt_flag pragmas:

How do I create nested subroutines?

Please refer to "pdds/pdd20_lexical_vars.pod/Nested Subroutines Have Outies; the ":outer" attribute" in docs for details.

Variables

How do I fetch a variable from the global namespace?

Use the get_root_global or get_hll_global op:

How can I delete a global?

You can retrieve the namespace hash and use the delete opcode.

How do I use lexical pads to have both a function scope and a global scope?

Please refer to "pdds/pdd20_lexical_vars.pod" in docs for details.

How can I delete a lexical variable?

You can't. You can store a PMCNULL as the value though, which will catch all further access to that variable and throw an exception. (You can create a PMCNULL with the null opcode.)

How do I resolve a variable name?

Use find_name:

This will find the name foo in the lexical, global, or builtin namespace, in that order, and store it in $P0.

How do I fetch a variable from the current lexical pad?

or much better, if possible just use the variable defined along with the .lex definition of foo.

How do I fetch a variable from any nesting depth?

That is still the same:

This finds a foo variable at any outer depth starting from the top.

If your language looks up variables differently, you have to walk the 'caller' chain. See also t/dynpmc/dynlexpad.t.

How can I produce more efficient code for lexicals?

Don't emit store_lex at all. Use find_lex only if the compiler doesn't know the variable. You can always just use the register that was defined in the .lex directive as an alias to that lexical, if you are in the same scope.

Modules, Classes, and Objects

How do I create a module?

XXX

How do I create a class?

With the newclass op:

How do I add instance variables/attributes?

Each class knows which attributes its objects can have. You can add attributes to a class (not to individual objects) like so:

How do I add instance methods to a class?

Methods are declared as functions in the class namespace with the :method keyword appended to the function declaration:

How do I override a vtable on a class?

As with methods, but note the new keyword. The vtable name specified must be an existing vtable slot.

Now, given an instance of NearlyPi in $P0

How do I access attributes?

You can access attributes by a short name:

When should I use properties vs. attributes?

Properties aren't inherited. If you have some additional data that don't fit into the class's hierarchy, you could use properties.

How do I create a class that is a subclass of another class?

You first have to get the class PMC of the class you want to subclass. Either you use the PMC returned by the newclass op if you created the class, or use the get_class op:

Then you can use the subclass op to create a new class that is a subclass of this class:

This stores the newly created class PMC in $P1.

How do I create a class that has more than one parent class?

First, create a class without a parent class using newclass (or with only one subclass, see previous question). Then add the other parent classes to it. Please refer to the next question for an example.

How do I add another parent class to my class?

If you have a class PMC (created with newclass or by subclass), you can add more parent classes to it with the addparent op:

How can I specify the constructor of a class?

Just override the init vtable for that class.

Or you can specify the constructor method by setting the BUILD property of the class PMC:

How do I instantiate a class?

You can do so either with the class name:

or with the class object:

The chief difference is that using a string constant will produce the specific error "Class 'Dog' not found" if that happens to be the case; the other code has to check explicitly.

During the new opcode the constructor is called.

How can I pass arguments to a constructor?

You can pass only a single argument to a constructor. By convention, a hash PMC is passed to the constructor that contains the arguments as key/value pairs:

How do I add module/class methods?

XXX

How do I access module/class variables?

XXX

Exceptions

How do I throw an exception in PIR?

The easiest way is the perl-like

You can also explicitly create an exception object and throw it:

How do I catch an exception in PIR?

Use push_eh to push an exception handler onto the stack. End the set of instructions that might throw the exception you're interested in with pop_eh.

An exception handler is called with one argument, which is the exception object. The message of the exception can be easily extracted, as follows:

How do I let exceptions from exit pass through my handler?

Rethrow the exception if it has a severity of EXCEPT_EXIT.

Exception example:

C Extensions

How do I create PMCs for my compiler?

Parrot supports dynamic PMCs, loadable at runtime, to allow compiler writers to extend Parrot with additional types. For more information about writing PMCs, see "build/pmc2c.pl" in tools and "pmc.pod" in docs.

See "dynpmc/Makefile" in src for an example of how to build your dynamic PMCS.

How do I add another op to Parrot?

Parrot supports dynamic op libraries. These allow for ops specific to one language to be used without having to place them into the Parrot core itself. For examples of dynamic op libraries, see "dynoplibs" in src.

How do I use the Native Calling Interface (NCI)?

Using the NCI you can invoke functions written in C from a Parrot script. To every NCI invocation, there are two parts: the native function to be invoked, and the PIR code to do the invocation.

First the native function, to be written in C. On Windows, it is necessary to do a DLL export specification of the NCI function:

/* foo.c */

/* specify the function prototype */
#ifdef __WIN32
__declspec(dllexport) void foo(void);
#else
void foo(void);
#endif

void foo(void) {
  printf("Hello Parrot!\n");
}

Then, after having compiled the file as a shared library, the PIR code looks like this:

If you embedded a Parrot in your C file and you want to invoke another function in that same C file, you should pass a null string to loadlib. Do that as follows:

Under Linux, the .c file must then be linked with the -export-dynamic option.

Misc

How can I access a program's environment?

Create a new Env PMC and access it like a hash.

How can I access Parrot's configuration?

See config_lib.pasm for all the keys in the config hash - or iterate over the config hash.