From 15ed53b1d14876a0db7885a0d34b3d20f94adaf5 Mon Sep 17 00:00:00 2001 From: Andreas Baumann Date: Tue, 28 Jul 2020 21:14:01 +0200 Subject: asm-386 is working on simple ELF test --- ecomp-c/asm-i386.c | 70 +++++++++++++++++++++++++++++------------------------- 1 file changed, 38 insertions(+), 32 deletions(-) diff --git a/ecomp-c/asm-i386.c b/ecomp-c/asm-i386.c index a79d03e..83904c5 100644 --- a/ecomp-c/asm-i386.c +++ b/ecomp-c/asm-i386.c @@ -72,8 +72,8 @@ enum { }; static int DEBUG_GETCHAR = 0; -static int DEBUG_SCANNER = 1; -static int DEBUG_PARSER = 1; +static int DEBUG_SCANNER = 0; +static int DEBUG_PARSER = 0; /* scanner */ @@ -813,7 +813,7 @@ typedef struct OperandInfo { int num; char *str; Register reg; - int addr; +/* int addr; */ /* Symbol *symbol; */ struct OperandInfo *next; struct ExpressionNode *node; @@ -1212,7 +1212,7 @@ static OperandInfo *parseOperand( OpcodeInfo *opcode_info ) operand_info = Allocate( sizeof( OperandInfo ) ); operand_info->str = NULL; operand_info->next = NULL; - operand_info->addr = ADDRESS_UNDEFINED; + operand_info->num = ADDRESS_UNDEFINED; operand_info->node = NULL; if( sym == S_ident ) { @@ -1482,18 +1482,24 @@ static OpcodeInfo *parseAssignment( void ) symbol->defined = 1; node = create_expression_node( ); - node->type = EXPRESSION_NODE_TYPE_VAR; - node->symbol = symbol; + node->type = EXPRESSION_NODE_TYPE_OP; + node->op = sym; + + node->left = create_expression_node( ); + node->left->type = EXPRESSION_NODE_TYPE_VAR; + node->left->symbol = symbol; sym = getSym( ); Expect( S_equals ); + node->right = parseExpression( ); + operand_info = Allocate( sizeof( OperandInfo ) ); operand_info->next = NULL; operand_info->type = OPERAND_ABSOLUTE; operand_info->num = 0; operand_info->str = NULL; - operand_info->node = parseExpression( ); + operand_info->node = node; append_operand( opcode_info, operand_info ); @@ -1527,7 +1533,7 @@ static OpcodeInfo *parseDirective( void ) opcode_info = parseOperation( last_label ); last_label = NULL; } else if( peek == S_equals ) { - parseAssignment( ); + opcode_info = parseAssignment( ); } else { opcode_info = parseOperation( last_label ); last_label = NULL; @@ -1622,10 +1628,10 @@ static int patchup_addresses( OpcodeInfo *opcode_info, int ORG ) */ switch( opcode->opcode ) { case OPCODE_JMP: - if( opcode->addr != ADDRESS_UNDEFINED && opcode->operand->addr != ADDRESS_UNDEFINED ) { - int rel = relative_distance( opcode, opcode->addr, opcode->operand->addr ); + if( opcode->addr != ADDRESS_UNDEFINED && opcode->operand->num != ADDRESS_UNDEFINED ) { + int rel = relative_distance( opcode, opcode->addr, opcode->operand->num ); if( DEBUG_PARSER ) { - fprintf( stderr, "JMP at $%X to $%X, rel %d\n", opcode->addr, opcode->operand->addr, rel ); + fprintf( stderr, "JMP at $%X to $%X, rel %d\n", opcode->addr, opcode->operand->num, rel ); } if( opcode->size == 2 ) { if( rel >= -128 && rel <= 127 ) { @@ -1651,8 +1657,8 @@ static int patchup_addresses( OpcodeInfo *opcode_info, int ORG ) case OPCODE_JBE: case OPCODE_JA: case OPCODE_JAE: - if( opcode->addr != ADDRESS_UNDEFINED && opcode->operand->addr != ADDRESS_UNDEFINED ) { - int rel = relative_distance( opcode, opcode->addr, opcode->operand->addr ); + if( opcode->addr != ADDRESS_UNDEFINED && opcode->operand->num != ADDRESS_UNDEFINED ) { + int rel = relative_distance( opcode, opcode->addr, opcode->operand->num ); if( rel >= -128 && rel <= 127 ) { /* all fine, short rel8 jump */ } else { @@ -1673,13 +1679,13 @@ static int patchup_addresses( OpcodeInfo *opcode_info, int ORG ) * correct address */ operand = opcode->operand; while( operand != NULL ) { - if( operand->type == OPERAND_MEMORY_DIRECT || operand->type == OPERAND_MEMORY_INDIRECT ) { - if( operand->addr == ADDRESS_UNDEFINED ) { +/* if( operand->type == OPERAND_MEMORY_DIRECT || operand->type == OPERAND_MEMORY_INDIRECT ) { */ + if( operand->num == ADDRESS_UNDEFINED ) { if( operand->node != NULL ) { - operand->addr = evaluateExpression( operand->node, ORG, opcode->addr ); + operand->num = evaluateExpression( operand->node, ORG, opcode->addr ); } } - } +/* } */ operand = operand->next; } opcode = opcode->next; @@ -1755,12 +1761,12 @@ static void print_opcodes( OpcodeInfo *opcode_info ) case OPERAND_MEMORY_DIRECT: fprintf( stderr, "%s%s=$%X", indent, get_expression_comment( operand->node, buf, MAX_STRING_LEN ), - operand->addr ); + operand->num ); break; case OPERAND_MEMORY_INDIRECT: fprintf( stderr, "%s[%s=$%X]", indent, get_expression_comment( operand->node, buf, MAX_STRING_LEN ), - operand->addr ); + operand->num ); break; case OPERAND_REGISTER_INDIRECT: fprintf( stderr, "%s[%s]", indent, registername[operand->reg] ); @@ -1872,10 +1878,10 @@ static int compute_addresses( OpcodeInfo *opcode_info ) case OPERAND_MEMORY_DIRECT: case OPERAND_MEMORY_INDIRECT: if( operand->node != NULL ) { - operand->addr = evaluateExpression( operand->node, ORG, LC ); + operand->num = evaluateExpression( operand->node, ORG, LC ); } if( DEBUG_PARSER ) { - fprintf( stderr, "LC=$%X assigned to operand at $%X\n", LC, operand->addr ); + fprintf( stderr, "LC=$%X assigned to operand at $%X\n", LC, operand->num ); } break; case OPERAND_REGISTER: @@ -1990,7 +1996,7 @@ static void emit_opcode( OpcodeInfo *opcode_info ) } else { Abort( "mov is only allowed with EAX or AL register" ); } - Emit_double_little_endian( opcode_info->operand->addr ); + Emit_double_little_endian( opcode_info->operand->num ); } else if( opcode_info->operand->type == OPERAND_REGISTER && opcode_info->operand->next->type == OPERAND_MEMORY_INDIRECT ) { if( opcode_info->operand->reg == REGISTER_EAX ) { @@ -2000,13 +2006,13 @@ static void emit_opcode( OpcodeInfo *opcode_info ) } else { Abort( "mov is only allowed with EAX or AL register" ); } - Emit_double_little_endian( opcode_info->operand->next->addr ); + Emit_double_little_endian( opcode_info->operand->next->num ); } else if( opcode_info->operand->type == OPERAND_REGISTER && opcode_info->operand->reg == REGISTER_EAX && opcode_info->operand->next->type == OPERAND_MEMORY_DIRECT ) { Emit( "%c", 0xB8 ); - Emit_double_little_endian( opcode_info->operand->next->addr ); + Emit_double_little_endian( opcode_info->operand->next->num ); } else if( opcode_info->operand->type == OPERAND_REGISTER_INDIRECT && opcode_info->operand->reg == REGISTER_EBX && opcode_info->operand->next->type == OPERAND_REGISTER && @@ -2084,7 +2090,7 @@ static void emit_opcode( OpcodeInfo *opcode_info ) break; case OPCODE_JMP: if( opcode_info->operand->type == OPERAND_MEMORY_DIRECT ) { - int rel = relative_distance( opcode_info, opcode_info->addr, opcode_info->operand->addr ); + int rel = relative_distance( opcode_info, opcode_info->addr, opcode_info->operand->num ); if( rel >= -128 && rel <= 127 ) { Emit( "%c", 0xEB ); Emit_char( rel ); @@ -2096,32 +2102,32 @@ static void emit_opcode( OpcodeInfo *opcode_info ) break; case OPCODE_JE: if( opcode_info->operand->type == OPERAND_MEMORY_DIRECT ) { - Emit( "%c%c", 0x74, relative_distance( opcode_info, opcode_info->addr, opcode_info->operand->addr ) ); + Emit( "%c%c", 0x74, relative_distance( opcode_info, opcode_info->addr, opcode_info->operand->num ) ); } break; case OPCODE_JNE: if( opcode_info->operand->type == OPERAND_MEMORY_DIRECT ) { - Emit( "%c%c", 0x75, relative_distance( opcode_info, opcode_info->addr, opcode_info->operand->addr ) ); + Emit( "%c%c", 0x75, relative_distance( opcode_info, opcode_info->addr, opcode_info->operand->num ) ); } break; case OPCODE_JB: if( opcode_info->operand->type == OPERAND_MEMORY_DIRECT ) { - Emit( "%c%c", 0x72, relative_distance( opcode_info, opcode_info->addr, opcode_info->operand->addr ) ); + Emit( "%c%c", 0x72, relative_distance( opcode_info, opcode_info->addr, opcode_info->operand->num ) ); } break; case OPCODE_JBE: if( opcode_info->operand->type == OPERAND_MEMORY_DIRECT ) { - Emit( "%c%c", 0x76, relative_distance( opcode_info, opcode_info->addr, opcode_info->operand->addr ) ); + Emit( "%c%c", 0x76, relative_distance( opcode_info, opcode_info->addr, opcode_info->operand->num ) ); } break; case OPCODE_JA: if( opcode_info->operand->type == OPERAND_MEMORY_DIRECT ) { - Emit( "%c%c", 0x77, relative_distance( opcode_info, opcode_info->addr, opcode_info->operand->addr ) ); + Emit( "%c%c", 0x77, relative_distance( opcode_info, opcode_info->addr, opcode_info->operand->num ) ); } break; case OPCODE_JAE: if( opcode_info->operand->type == OPERAND_MEMORY_DIRECT ) { - Emit( "%c%c", 0x73, relative_distance( opcode_info, opcode_info->addr, opcode_info->operand->addr ) ); + Emit( "%c%c", 0x73, relative_distance( opcode_info, opcode_info->addr, opcode_info->operand->num ) ); } break; case OPCODE_INT: @@ -2131,7 +2137,7 @@ static void emit_opcode( OpcodeInfo *opcode_info ) break; case OPCODE_CALL: if( opcode_info->operand->type == OPERAND_MEMORY_DIRECT ) { - int rel = relative_distance( opcode_info, opcode_info->addr, opcode_info->operand->addr ); + int rel = relative_distance( opcode_info, opcode_info->addr, opcode_info->operand->num ); Emit( "%c", 0xE8 ); Emit_double_little_endian( rel ); } -- cgit v1.2.3-54-g00ecf