|
From: Keith J. <kja...@ag...> - 2002-05-02 19:58:50
|
I've started implementing functions. It's currently in what I'll call a
'lame' state. If you run:
1;
decl whatever(bleh)
{
return 7;
}
2;
through the compiler, you'll see it just compiles functions by after the
PUSH 1, POP, adding a JMP to the PUSH 2, POP, effectively skipping the
function code. This is Keith being lazy for the time being. In the future
compiler pass after parse and before compile all the functions will be
moved to the end, so they are compiled after all the code, then the need
for the 'jump past the function' code will be removed.
Also, currently it's just compiling the function. There's no symbol table
for what address it starts at, so the CALL opcode obviously isn't
implemented. :) THey are compiled, that's all :)
Keith
At 03:30 PM 5/2/2002, Keith Jackson wrote:
>Update of /cvsroot/cup-language/cup/src/compiler
>In directory usw-pr-cvs1:/tmp/cvs-serv3407/compiler
>
>Modified Files:
> binary.c compiler.h nodes.c parser.y
>Log Message:
>Functions partially implemented
>
>
>
>Index: binary.c
>===================================================================
>RCS file: /cvsroot/cup-language/cup/src/compiler/binary.c,v
>retrieving revision 1.3
>retrieving revision 1.4
>diff -C2 -r1.3 -r1.4
>*** binary.c 2 May 2002 14:16:31 -0000 1.3
>--- binary.c 2 May 2002 19:30:56 -0000 1.4
>***************
>*** 68,71 ****
>--- 68,72 ----
> unsigned long lstart;
> CUPCNODE *casenode, *defaultnode;
>+ CUPCNODE *argnode;
>
> switch (node->op)
>***************
>*** 374,377 ****
>--- 375,411 ----
> cupc_code_write(comp, CUPOP_JMP);
> cupc_code_write_data(comp, &cjmp, sizeof(unsigned long));
>+ break;
>+ case CUPCOP_RETURN:
>+ // PUSH arg (optional?)
>+ // RET
>+ if (node->one)
>+ cupc_bin_build(comp, node->one, 0, 0, 0);
>+ cupc_code_write(comp, CUPOP_RET);
>+ break;
>+ case CUPCOP_FUNCTION:
>+ // JMP label1
>+ // ARG blah
>+ // ARG y
>+ // code
>+ // RET
>+ lstart = comp->codelen;
>+ label1 = cupc_code_new_label();
>+ cupc_code_write(comp, CUPOP_JMP);
>+ cupc_code_write_data(comp, &label1, sizeof(unsigned long));
>+ for (argnode = node->two; argnode && argnode->next; argnode =
>argnode->next)
>+ ;
>+ while (argnode)
>+ {
>+ cupc_code_write(comp, CUPOP_PUSH);
>+ cupc_code_write_data(comp, &comp->datalen, sizeof(unsigned long));
>+ cupc_data_write(comp, CUPDATA_VARIABLE);
>+ cupc_data_write_data(comp, argnode->const_string,
>strlen(argnode->const_string) + 1);
>+ argnode = argnode->prev;
>+ }
>+ if (node->three)
>+ cupc_bin_build(comp, node->three, 0, 0, 0);
>+ cupc_code_write(comp, CUPOP_RET);
>+ cupc_code_fix_jumps(comp, label1, comp->codelen, lstart);
>+ cupc_code_free_label(label1);
> break;
> }
>
>Index: compiler.h
>===================================================================
>RCS file: /cvsroot/cup-language/cup/src/compiler/compiler.h,v
>retrieving revision 1.6
>retrieving revision 1.7
>diff -C2 -r1.6 -r1.7
>*** compiler.h 2 May 2002 14:16:32 -0000 1.6
>--- compiler.h 2 May 2002 19:30:56 -0000 1.7
>***************
>*** 27,32 ****
> CUPCOP_NOP = 0,
>
>! CUPCOP_FUNCTION, // Not implemented
>! CUPCOP_CALL, // Not implemented
>
> CUPCOP_PUSH_FLOAT,
>--- 27,33 ----
> CUPCOP_NOP = 0,
>
>! CUPCOP_FUNCTION,
>! CUPCOP_CALL,
>! CUPCOP_RETURN,
>
> CUPCOP_PUSH_FLOAT,
>
>Index: nodes.c
>===================================================================
>RCS file: /cvsroot/cup-language/cup/src/compiler/nodes.c,v
>retrieving revision 1.4
>retrieving revision 1.5
>diff -C2 -r1.4 -r1.5
>*** nodes.c 2 May 2002 14:16:32 -0000 1.4
>--- nodes.c 2 May 2002 19:30:56 -0000 1.5
>***************
>*** 81,85 ****
> LIST *list;
>
>! if (!comp->nodestack || list_length(comp->nodestack) < 1)
> return NULL;
>
>--- 81,85 ----
> LIST *list;
>
>! if (!comp->nodestack)
> return NULL;
>
>***************
>*** 87,91 ****
> ret = list_item_pop_front(list);
> node = ret;
>! while ((node->next = list_item_pop_front(list)))
> {
> node->next->prev = node;
>--- 87,91 ----
> ret = list_item_pop_front(list);
> node = ret;
>! while (node && (node->next = list_item_pop_front(list)))
> {
> node->next->prev = node;
>
>Index: parser.y
>===================================================================
>RCS file: /cvsroot/cup-language/cup/src/compiler/parser.y,v
>retrieving revision 1.8
>retrieving revision 1.9
>diff -C2 -r1.8 -r1.9
>*** parser.y 2 May 2002 14:16:32 -0000 1.8
>--- parser.y 2 May 2002 19:30:56 -0000 1.9
>***************
>*** 45,49 ****
> %type <node> statement lang_block lang_while lang_for lang_continue
> lang_break expr flt intgr string lang_if lang_switch variable
>! function funccall
>
> /* Old
>--- 45,49 ----
> %type <node> statement lang_block lang_while lang_for lang_continue
> lang_break expr flt intgr string lang_if lang_switch variable
>! function funccall lang_return
>
> /* Old
>***************
>*** 125,128 ****
>--- 125,129 ----
> | lang_if { $$ = $1; }
> | lang_switch { $$ = $1; }
>+ | lang_return { $$ = $1; }
> ;
>
>***************
>*** 165,168 ****
>--- 166,175 ----
> lang_break: TOKEN_BREAK ';' { $$ = cupc_node_new(CUPCOP_BREAK, NULL, NULL,
> NULL, NULL); }
>+ ;
>+
>+ lang_return: TOKEN_RETURN ';' { $$ = cupc_node_new(CUPCOP_RETURN, NULL,
>NULL,
>+ NULL, NULL); }
>+ | TOKEN_RETURN expr ';' { $$ = cupc_node_new(CUPCOP_RETURN, $2,
>+ NULL, NULL, NULL); }
> ;
>
>
>
>_______________________________________________________________
>
>Have big pipes? SourceForge.net is looking for download mirrors. We supply
>the hardware. You get the recognition. Email Us: ban...@so...
|