Skip to content

Commit

Permalink
few updates to docs, and fix return instructions for functions. works…
Browse files Browse the repository at this point in the history
… in the perl m0 assembler.
  • Loading branch information
kjs committed Jun 7, 2012
1 parent bb931ec commit f9366b9
Show file tree
Hide file tree
Showing 2 changed files with 120 additions and 36 deletions.
121 changes: 112 additions & 9 deletions docs/pddxx_m1.pod
Expand Up @@ -12,7 +12,20 @@ and a computation model. Details of M0 can be found in [1].

=head2 Design Principles of M1

A few basic design principles (so-called I<key drivers>) drove the design
of M1:

=over 4

=item * Maintainability

=item * High-quality output (i.e., M0 instructions)

=item * Extensibility (e.g., easy to add another phase to the compilation process)

=item * Ease of implementation

=back

=head3 Differences with C

Expand All @@ -28,9 +41,13 @@ Compared to C, M1 has:

=item * a simpler (and more restrictive) grammar;

=item * no union construct
=item * no union construct {{XXX do we want that?}}

=item *
=item * no C<goto> statement (to prevent spaghetti code)

=item * namespaces {{XXX do we want that?}}

=item * a try-catch statement {{XXX if we get that working}}

=back

Expand All @@ -41,8 +58,48 @@ Other differences with C include the following.

Completed milestones:

=over 4

=item * assignments

=item * iterating statements (while, do-while, for)

=item * conditional statements (if/else, switch)

=item * one-dimensional arrays; read and write

=item * simple structs; read and write

=back

Incomplete milestones:

=over 4

=item * function calls and returns

=item * Enumerations

=item * PMC definitions

=item * methods (as part of a PMC)

=item * vtable methods (virtual methods)

=item * PMC inheritance

=item * namespaces

=item * semantic checker

=item * module system (preprocessor?)

=item * register allocator

=item * optimization phase

=back

=head2 Architecture overview

The M1 compiler follows the so-called C<batch-sequential> architecture> [2], which
Expand Down Expand Up @@ -76,6 +133,9 @@ later phases.
After the parse, the AST is passed on to the semantic analysis phase. In this phase,
the compiler performs a number of type checks.

After the type checking phase, the code generation phase traverses the AST, and
generates appropriate instructions for each node.



=head2 Implementation
Expand Down Expand Up @@ -150,7 +210,17 @@ language-dependent behavior. A PMC can be compared to a C++ class definition.

=head3 Variable declarations

=head3 Struct type definitions
=head3 Assignments

=head3 Arrays

Arrays, when declared, are auto-vivivied. This means that code is generated to
allocate memory for them.

=head3 Structs


=head3 User-defined type definitions



Expand All @@ -161,17 +231,22 @@ language-dependent behavior. A PMC can be compared to a C++ class definition.
The following words are reserved and cannot be used as identifier:

bool
case
char
const
default
do
else
false
for
if
int
num
pmc
string
struct
switch
true
while

=head3 M1 Grammar
Expand All @@ -185,11 +260,20 @@ M1-program: chunk+
chunk: function-def
| pmc-def
| struct-def
| enum-def
| namespace

struct-def: struct NAME { struct-member+ }

struct-member: type NAME ;

function-def: type NAME ( parameters? ) block

parameters: parameter [, parameter]*

enum-def: enum NAME { enum-constant [, enum-constant]* }

enum-constant: NAME

block: { statement* }

Expand All @@ -199,11 +283,14 @@ statement: assignment
| do-while-stat
| break-stat
| for-stat
| switch-stat
| block
| var-decl
| const-decl

assignment: target = expression ;
assignment: target assignop expression ;

assignop: = | += | -= | *= | /= | %= | >>= | <<= | &= | |=

if-stat: if ( expression ) statement [else statement]?

Expand All @@ -214,23 +301,39 @@ do-while-stat: do { statement* } while ( expression ) ;
for-statement: for ( for-init? ; for-cond? ; for-step? ) statement

break-stat: break

switch-stat: switch ( expression ) { case* default-case? }

case: case INTEGER : statement*

default-case: default : statement *

expression: INTEGER
| NUMBER
| STRING
| CHAR
| true
| false
| expression ? expression : expression
| expression binop expression
| unop expression
| target
| expression ++
| expression --
| ++ expression
| -- expression
| target ++
| target --
| ++ target
| -- target


binop: + | - | * | % | / | >> | << | > | < | >= | <= | == | != | = | & | "|"


unop: !
| -

target: target field-access*

field-access: . field
| [ expression ]
| "[" expression "]"



Expand Down
35 changes: 8 additions & 27 deletions src/gencode.c
Expand Up @@ -1606,33 +1606,14 @@ gencode_chunk_return(M1_compiler *comp, m1_chunk *chunk) {

/* XXX only generate in non-main functions. */

if (strcmp(chunk->name, "main") != 0) {

m1_reg t = gen_reg(comp, VAL_INT);
m1_reg parent_cf = gen_reg(comp, VAL_CHUNK);
fprintf(OUT, "\tset_imm I%d, 0, PCF\n", t.no);
fprintf(OUT, "\tderef P%d, CF, I%d\n", parent_cf.no, t.no);

m1_reg t2 = gen_reg(comp, VAL_INT);
fprintf(OUT, "\tset_imm I%d, 0, RETPC\n", t2.no);
fprintf(OUT, "\tderef I%d, P%d, I%d\n", t2.no, parent_cf.no, t2.no);

m1_reg callingfun_index = gen_reg(comp, VAL_INT);
m1_reg const_seg_index = gen_reg(comp, VAL_INT);
m1_reg parent_const_seg = gen_reg(comp, VAL_CHUNK);
m1_reg zero_reg = gen_reg(comp, VAL_INT);

fprintf(OUT, "\tset_imm I%d, 0, CONSTS\n", const_seg_index.no);
fprintf(OUT, "\tderef P%d, P%d, I%d\n", parent_const_seg.no, parent_cf.no, const_seg_index.no);
fprintf(OUT, "\tset_imm I%d, 0, 0\n", zero_reg.no);
fprintf(OUT, "\tderef I%d, P%d, I%d\n", callingfun_index.no, parent_const_seg.no, zero_reg.no);
fprintf(OUT, "\tgoto_chunk I%d, I%d, x\n", callingfun_index.no, t2.no);

// int callfunidx = 2;
// fprintf(OUT, "\tset_imm I%d, 0, %d\n", t.no, callfunidx);
// fprintf(OUT, "\tderef I%d, CONSTS, I%d\n", t.no, t.no);
// fprintf(OUT, "\tgoto_chunk I%d, I%d, x\n", t.no, t2.no);

if (strcmp(chunk->name, "main") != 0) {
m1_reg retpc_reg = gen_reg(comp, VAL_INT);
m1_reg chunk_index = gen_reg(comp, VAL_INT);

fprintf(OUT, "\tderef I%d, PCF, RETPC\n", retpc_reg.no);
fprintf(OUT, "\tset_imm I%d, 0, CHUNK\n", chunk_index.no);
fprintf(OUT, "\tderef I%d, PCF, I%d\n", chunk_index.no, chunk_index.no);
fprintf(OUT, "\tgoto_chunk I%d, I%d, x\n", chunk_index.no, retpc_reg.no);
}
}

Expand Down

0 comments on commit f9366b9

Please sign in to comment.