summaryrefslogtreecommitdiff
path: root/miniany/cc.c
diff options
context:
space:
mode:
Diffstat (limited to 'miniany/cc.c')
-rw-r--r--miniany/cc.c27
1 files changed, 23 insertions, 4 deletions
diff --git a/miniany/cc.c b/miniany/cc.c
index 5cf2f9c..c630a8f 100644
--- a/miniany/cc.c
+++ b/miniany/cc.c
@@ -1411,11 +1411,13 @@ void parseStatementBlock( struct Compiler *compiler )
parserExpect( parser, S_RBRACE, "}" );
}
-void parseFunctionDeclaraion( struct Compiler *compiler )
+void parseFunctionDeclaration( struct Compiler *compiler )
{
struct Parser *parser;
+ struct Symbol *sym;
parser = compiler->parser;
+
if( parser->token == S_IDENT ) {
if( strcmp( parser->scanner->ident, "void" ) != 0 ) {
scannerPrintErrorHeader( parser->scanner );
@@ -1424,18 +1426,35 @@ void parseFunctionDeclaraion( struct Compiler *compiler )
exit( EXIT_FAILURE );
}
}
- parser->token = getToken( parser->scanner );
+
+ parserExpect( parser, S_IDENT, "identifier" );
+ sym = getSymbol( parser->global_scope, parser->scanner->ident );
+ if( sym == NULL ) {
+ sym = createSymbol( parser->scanner->ident );
+ insertSymbol( parser->global_scope, sym );
+ } else {
+ scannerPrintErrorHeader( parser->scanner );
+ putstring( "duplicate global symbol '" );
+ putstring( parser->scanner->ident );
+ putstring( "'" );
+ putnl( );
+ exit( EXIT_FAILURE );
+ }
if( parser->debug ) {
putstring( "; function declaration '" );
putstring( parser->scanner->ident );
putstring( "'" );
putnl( );
}
+
putstring( parser->scanner->ident ); putchar( ':' ); putnl( );
+ putstring( "push ebp" ); putnl( );
+ putstring( "mov ebp, esp" ); putnl( );
parser->token = getToken( compiler->parser->scanner );
parserExpect( parser, S_LPAREN, "(" );
parserExpect( parser, S_RPAREN, ")" );
parseStatementBlock( compiler );
+ putstring( "pop ebp" ); putnl( );
putstring( "ret" ); putnl( );
}
@@ -1467,11 +1486,11 @@ int main( int argc, char **argv )
compiler = createCompiler( );
/* compiler->parser->scanner->debug = 1; */
- /* compiler->parser->debug = 1; */
+ compiler->parser->debug = 1;
/* compiler->generator->debug = 1; */
genPrologue( compiler );
compiler->parser->token = getToken( compiler->parser->scanner );
- parseFunctionDeclaraion( compiler );
+ parseFunctionDeclaration( compiler );
genEpilogue( compiler );
freeCompiler( compiler );