Skip to content

Commit

Permalink
Relations support and cli
Browse files Browse the repository at this point in the history
  • Loading branch information
sayon committed Aug 4, 2014
1 parent d495817 commit 6c69e19
Show file tree
Hide file tree
Showing 11 changed files with 126 additions and 122 deletions.
9 changes: 9 additions & 0 deletions dwarf/dwarf-compiler/AST.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ typedef enum {
AST_MINUS,
AST_MULT,
AST_DIVIDE,
AST_LT,
AST_EQ,
AST_GT,
AST_SEQ,
AST_IDENTIFIER,
AST_PRINT
Expand Down Expand Up @@ -77,6 +80,12 @@ ast_t* ast_plus( ast_t* left, ast_t* right );
ast_t* ast_minus( ast_t* left, ast_t* right );
ast_t* ast_mult( ast_t* left, ast_t* right );
ast_t* ast_div( ast_t* left, ast_t* right );

ast_t* ast_lt( ast_t* left, ast_t* right );
ast_t* ast_eq( ast_t* left, ast_t* right );
ast_t* ast_gt( ast_t* left, ast_t* right );


ast_t* ast_assignment( ast_t* ident, ast_t* expr );
ast_t* ast_seq( ast_t* left, ast_t* right );
ast_t* ast_identifier( char* name );
Expand Down
8 changes: 0 additions & 8 deletions dwarf/dwarf-compiler/ast_constructors.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,18 +41,10 @@ ast_t* ast_lt( ast_t* left, ast_t* right ) {
return ast_binop( left, right, AST_LT );
}

ast_t* ast_le( ast_t* left, ast_t* right ) {
return ast_binop( left, right, AST_LE );
}

ast_t* ast_eq( ast_t* left, ast_t* right ) {
return ast_binop( left, right, AST_EQ );
}

ast_t* ast_ge( ast_t* left, ast_t* right ) {
return ast_binop( left, right, AST_GE );
}

ast_t* ast_gt( ast_t* left, ast_t* right ) {
return ast_binop( left, right, AST_GT );
}
Expand Down
24 changes: 4 additions & 20 deletions dwarf/dwarf-compiler/ast_helpers.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,8 @@ void ast_visit( ast_t* node, void (before)( ast_t* node ), void (after)( ast_t*
case AST_MINUS:
case AST_MULT:
case AST_DIVIDE:
case AST_LT:
case AST_LE:
case AST_EQ:
case AST_GE:
case AST_LT:
case AST_EQ:
case AST_GT:
ast_visit( node->attributes.as_binop.left, before, after );
ast_visit( node->attributes.as_binop.right, before, after );
Expand Down Expand Up @@ -119,28 +117,14 @@ void ast_print( ast_t* node, FILE* out ) {
fprintf( out, " < " );
ast_print( node->attributes.as_binop.right, out );
fprintf( out, " )" );
break;
case AST_LE:
fprintf( out, "( " );
ast_print( node->attributes.as_binop.left, out );
fprintf( out, " <= " );
ast_print( node->attributes.as_binop.right, out );
fprintf( out, " )" );
break;
break;
case AST_EQ:
fprintf( out, "( " );
ast_print( node->attributes.as_binop.left, out );
fprintf( out, " == " );
ast_print( node->attributes.as_binop.right, out );
fprintf( out, " )" );
break;
case AST_GE:
fprintf( out, "( " );
ast_print( node->attributes.as_binop.left, out );
fprintf( out, " >= " );
ast_print( node->attributes.as_binop.right, out );
fprintf( out, " )" );
break;
break;
case AST_GT:
fprintf( out, "( " );
ast_print( node->attributes.as_binop.left, out );
Expand Down
53 changes: 41 additions & 12 deletions dwarf/dwarf-compiler/bytecode.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,19 @@ size_t bytecode_size[] = {
1,
1,
1,
1,
1 + sizeof( lang_int_t ),
1 + sizeof( reg_num_t ),
1 + sizeof( reg_num_t ),
1 + sizeof( addr_offset_t ),
1 + sizeof( addr_offset_t ),
1 + sizeof( addr_offset_t ),
1,
1,
1,
1
};

/* Helpers *//*
static bytecode_t* last( bytecode_t* bc )
{
while (bc != NULL) bc = bc->next;
return bc;
}
static void bc_concat( bytecode_t* left, bytecode_t* right ) {
last( left )-> next = right;
}*/

void bytecode_foreach( bytecode_t* bc, void ( action )( bytecode_t* ) ) {
bytecode_t* next;

Expand Down Expand Up @@ -57,7 +49,6 @@ bytecode_t* emit_bytecode( bytecode_builder_t* builder, opcode_t opcode) {
}



/* Helpers to emit specific bytecodes with parameters */

static addr_offset_t* emit_delayed_jump( bytecode_builder_t* builder, opcode_t code ) {
Expand Down Expand Up @@ -132,4 +123,42 @@ void bytecode_prettyprint( bytecode_t* bc ) {
bc = next;
}
}
static void bytecode_free_one( bytecode_t* bc ) { free (bc); }

void bytecode_free( bytecode_t* bc ) {
bytecode_foreach( bc, bytecode_free_one );
}


static void bytecode_flush_one( bytecode_t* bc, FILE* file ) {
fwrite( &(bc->opcode), 1, 1, file );

switch( bc->opcode ) {
case BC_IPUSHC:
fwrite( &(bc-> params.immediate), sizeof( lang_int_t ), 1, file );
break;
case BC_IPUSHREG:
case BC_IPOPREG:
fwrite( &(bc-> params.reg), sizeof( reg_num_t ), 1, file );
break;
case BC_JMP:
case BC_JZ:
fwrite( &(bc-> params.offset), sizeof( addr_offset_t ), 1, file );
break;
default:
break;

}
}

void bytecode_flush( bytecode_t* bc, FILE* file) {
bytecode_t* next;

if ( bc == NULL ) return;

while( bc != NULL ) {
next = bc->next;
bytecode_flush_one( bc, file );
bc = next;
}
}
11 changes: 5 additions & 6 deletions dwarf/dwarf-compiler/bytecode.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ typedef enum {
BC_JZ,
BC_JNZ,
BC_ICMP,
BC_EQ,
BC_PRINT,
BC_DEBUG
} opcode_t;
Expand All @@ -36,6 +37,7 @@ static char* bytecode_mnemonics[] = {
"JZ",
"JNZ",
"ICMP",
"EQ",
"PRINT",
"DEBUG"
};
Expand All @@ -61,11 +63,7 @@ typedef struct {
bytecode_t* last;
}
bytecode_builder_t;

typedef struct {
addr_offset_t* addr;
} addr_param_t;




bytecode_t* emit_bytecode( bytecode_builder_t* builder, opcode_t opcode );
Expand All @@ -90,5 +88,6 @@ void emit_ipopreg( bytecode_builder_t* builder, reg_num_t reg);
void bytecode_foreach( bytecode_t* bc, void ( action )( bytecode_t* ) );
void bytecode_print( bytecode_t* bytecode );
void bytecode_prettyprint( bytecode_t* bc );

void bytecode_free( bytecode_t* bc );
void bytecode_flush( bytecode_t* bc, FILE* file);
#endif
71 changes: 19 additions & 52 deletions dwarf/dwarf-compiler/codegen.c
Original file line number Diff line number Diff line change
Expand Up @@ -77,64 +77,33 @@ static size_t generate_div( ast_t* node, bytecode_builder_t* bc, generator_conte
return first + bytecode_size[ BC_IDIV ] + second;
}

static size_t generate_lt( ast_t* node, bytecode_builder_t* bc, generator_context_t* ctx ) {
static size_t generate_gt_helper( ast_t* left, ast_t* right, bytecode_builder_t* bc, generator_context_t* ctx ) {
size_t first, second;
first = generate( node->attributes.as_binop.right, bc, ctx );
second = generate( node->attributes.as_binop.left, bc, ctx );
first = generate( left, bc, ctx );
second = generate( right, bc, ctx );
emit_bytecode( bc, BC_ICMP );
return first + bytecode_size[ BC_ICMP ] + second;
}

static size_t generate_gt( ast_t* node, bytecode_builder_t* bc, generator_context_t* ctx ) {
size_t first, second;
first = generate( node->attributes.as_binop.left, bc, ctx );
second = generate( node->attributes.as_binop.right, bc, ctx );
emit_bytecode( bc, BC_ICMP );
return first + bytecode_size[ BC_ICMP ] + second;
}

static size_t generate_eq( ast_t* node, bytecode_builder_t* bc, generator_context_t* ctx ) {
size_t first, second;
first = generate( node->attributes.as_binop.left, bc, ctx );
second = generate( node->attributes.as_binop.right, bc, ctx );
emit_bytecode( bc, BC_ICMP );
emit_bytecode( bc, BC_DUP );
emit_bytecode( bc, BC_IMUL );
emit_ipushc( bc, -1 );
emit_ipushc( bc, BC_IADD );
emit_ipushc( bc, 1 );
emit_bytecode( bc, BC_EQ );
return first + second
+ bytecode_size[ BC_ICMP ]
+ bytecode_size[ BC_DUP ]
+ bytecode_size[ BC_IMUL ]
+ bytecode_size[ BC_IPUSHC ]
+ bytecode_size[ BC_IADD ] ;
+ bytecode_size[ BC_EQ ];
}

static size_t generate_le( ast_t* node, bytecode_builder_t* bc, generator_context_t* ctx ) {
size_t first, second;
first = generate( node->attributes.as_binop.right, bc, ctx );
second = generate( node->attributes.as_binop.left, bc, ctx );
emit_bytecode( bc, BC_ICMP );
emit_ipushc( bc, 1 );
emit_ipushc( bc, BC_IADD );
return first + second
+ bytecode_size[ BC_ICMP ]
+ bytecode_size[ BC_IPUSHC ]
+ bytecode_size[ BC_IADD ] ;
static size_t generate_gt( ast_t* node, bytecode_builder_t* bc, generator_context_t* ctx ) {
return generate_gt_helper( node->attributes.as_binop.left, node->attributes.as_binop.right, bc, ctx );
}

static size_t generate_lt( ast_t* node, bytecode_builder_t* bc, generator_context_t* ctx ) { //a < b === b > a
return generate_gt_helper( node->attributes.as_binop.right, node->attributes.as_binop.left, bc, ctx );
}

static size_t generate_ge( ast_t* node, bytecode_builder_t* bc, generator_context_t* ctx ) {
size_t first, second;
first = generate( node->attributes.as_binop.left, bc, ctx );
second = generate( node->attributes.as_binop.right, bc, ctx );
emit_bytecode( bc, BC_ICMP );
emit_ipushc( bc, 1 );
emit_ipushc( bc, BC_IADD );
return first + second
+ bytecode_size[ BC_ICMP ]
+ bytecode_size[ BC_IPUSHC ]
+ bytecode_size[ BC_IADD ] ;

static size_t generate_eq( ast_t* node, bytecode_builder_t* bc, generator_context_t* ctx ) {
size_t parts;
parts = generate( node->attributes.as_binop.left, bc, ctx ) + generate( node->attributes.as_binop.right, bc, ctx );
emit_bytecode( bc, BC_EQ );
return parts + bytecode_size[ BC_EQ ];
}

static size_t generate_seq( ast_t* node, bytecode_builder_t* bc, generator_context_t* ctx ) {
Expand Down Expand Up @@ -214,10 +183,8 @@ static size_t generate( ast_t* node, bytecode_builder_t* bc, generator_context_t
case AST_MINUS: return generate_minus( node, bc, ctx );
case AST_MULT: return generate_mult( node, bc, ctx );
case AST_DIVIDE: return generate_div( node, bc, ctx );
case AST_LT: return generate_lt( node, bc, ctx );
case AST_LE: return generate_le( node, bc, ctx );
case AST_EQ: return generate_eq( node, bc, ctx );
case AST_GE: return generate_ge( node, bc, ctx );
case AST_LT: return generate_lt( node, bc, ctx );
case AST_EQ: return generate_eq( node, bc, ctx );
case AST_GT: return generate_gt( node, bc, ctx );
case AST_SEQ: return generate_seq( node, bc, ctx );
case AST_IDENTIFIER: return generate_identifier( node, bc, ctx );
Expand Down
3 changes: 2 additions & 1 deletion dwarf/dwarf-compiler/dwarf-compiler.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@
<ClInclude Include="bytecode.h" />
<ClInclude Include="codegen.h" />
<ClInclude Include="common.h" />
<ClInclude Include="fileio.h" />
<ClInclude Include="io.h" />
<ClInclude Include="lexer.h" />
<ClInclude Include="parser.h" />
<ClInclude Include="tokens.h" />
Expand All @@ -93,6 +93,7 @@
<ClCompile Include="ast_helpers.c" />
<ClCompile Include="bytecode.c" />
<ClCompile Include="codegen.c" />
<ClCompile Include="io.c" />
<ClCompile Include="lexer.c" />
<ClCompile Include="main.c" />
<ClCompile Include="parser.c" />
Expand Down
5 changes: 4 additions & 1 deletion dwarf/dwarf-compiler/dwarf-compiler.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
<ClInclude Include="token_list.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="fileio.h">
<ClInclude Include="io.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
Expand Down Expand Up @@ -71,5 +71,8 @@
<ClCompile Include="token_list.c">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="io.c">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
</Project>
Loading

0 comments on commit 6c69e19

Please sign in to comment.