From: Peter Johnson Date: Sun, 16 Sep 2001 19:44:49 +0000 (-0000) Subject: Use bytecode * instead of bytecode in parser. X-Git-Tag: v0.1.0~348 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=3c2a67ca1c0dc8410e8bb2a574c2171eea180f61;p=yasm Use bytecode * instead of bytecode in parser. Rename and restructure bytecode functions to make this use clearer. svn path=/trunk/yasm/; revision=164 --- diff --git a/libyasm/bytecode.c b/libyasm/bytecode.c index 2765a251..e5954562 100644 --- a/libyasm/bytecode.c +++ b/libyasm/bytecode.c @@ -1,4 +1,4 @@ -/* $Id: bytecode.c,v 1.20 2001/08/30 03:45:26 peter Exp $ +/* $Id: bytecode.c,v 1.21 2001/09/16 19:44:49 peter Exp $ * Bytecode utility functions * * Copyright (C) 2001 Peter Johnson @@ -41,7 +41,7 @@ #include "bytecode.h" -RCSID("$Id: bytecode.c,v 1.20 2001/08/30 03:45:26 peter Exp $"); +RCSID("$Id: bytecode.c,v 1.21 2001/09/16 19:44:49 peter Exp $"); /* Static structures for when NULL is passed to conversion functions. */ /* for Convert*ToEA() */ @@ -53,7 +53,7 @@ static immval im_static; /* for Convert*ToBytes() */ unsigned char bytes_static[16]; -static void BuildBC_Common(bytecode *bc); +static bytecode *bytecode_new_common(void); effaddr * ConvertRegToEA(effaddr *ptr, unsigned long reg) @@ -246,9 +246,14 @@ SetOpcodeSel(jmprel_opcode_sel *old_sel, jmprel_opcode_sel new_sel) *old_sel = new_sel; } -static void -BuildBC_Common(bytecode *bc) +static bytecode * +bytecode_new_common(void) { + bytecode *bc = malloc(sizeof(bytecode)); + + if (!bc) + Fatal(FATAL_NOMEM); + bc->len = 0; bc->filename = strdup(filename); @@ -258,19 +263,20 @@ BuildBC_Common(bytecode *bc) bc->mode_bits = mode_bits; } -void -BuildBC_Insn(bytecode *bc, - unsigned char opersize, - unsigned char opcode_len, - unsigned char op0, - unsigned char op1, - unsigned char op2, - effaddr *ea_ptr, - unsigned char spare, - immval *im_ptr, - unsigned char im_len, - unsigned char im_sign) +bytecode * +bytecode_new_insn(unsigned char opersize, + unsigned char opcode_len, + unsigned char op0, + unsigned char op1, + unsigned char op2, + effaddr *ea_ptr, + unsigned char spare, + immval *im_ptr, + unsigned char im_len, + unsigned char im_sign) { + bytecode *bc = bytecode_new_common(); + bc->type = BC_INSN; if (ea_ptr) { @@ -303,24 +309,25 @@ BuildBC_Insn(bytecode *bc, bc->data.insn.opersize = opersize; bc->data.insn.lockrep_pre = 0; - BuildBC_Common(bc); + return bc; } -void -BuildBC_JmpRel(bytecode *bc, - targetval *target, - unsigned char short_valid, - unsigned char short_opcode_len, - unsigned char short_op0, - unsigned char short_op1, - unsigned char short_op2, - unsigned char near_valid, - unsigned char near_opcode_len, - unsigned char near_op0, - unsigned char near_op1, - unsigned char near_op2, - unsigned char addrsize) +bytecode * +bytecode_new_jmprel(targetval *target, + unsigned char short_valid, + unsigned char short_opcode_len, + unsigned char short_op0, + unsigned char short_op1, + unsigned char short_op2, + unsigned char near_valid, + unsigned char near_opcode_len, + unsigned char near_op0, + unsigned char near_op1, + unsigned char near_op2, + unsigned char addrsize) { + bytecode *bc = bytecode_new_common(); + bc->type = BC_JMPREL; bc->data.jmprel.target = target->val; @@ -351,12 +358,13 @@ BuildBC_JmpRel(bytecode *bc, bc->data.jmprel.opersize = 0; bc->data.jmprel.lockrep_pre = 0; - BuildBC_Common(bc); + return bc; } -void -BuildBC_Data(bytecode *bc, datavalhead *datahead, unsigned long size) +bytecode * +bytecode_new_data(datavalhead *datahead, unsigned long size) { + bytecode *bc; dataval *cur; /* First check to see if all the data elements are valid for the size @@ -395,27 +403,31 @@ BuildBC_Data(bytecode *bc, datavalhead *datahead, unsigned long size) } } + bc = bytecode_new_common(); + bc->type = BC_DATA; bc->data.data.datahead = *datahead; bc->data.data.size = size; - BuildBC_Common(bc); + return bc; } -void -BuildBC_Reserve(bytecode *bc, expr *numitems, unsigned long itemsize) +bytecode * +bytecode_new_reserve(expr *numitems, unsigned long itemsize) { + bytecode *bc = bytecode_new_common(); + bc->type = BC_RESERVE; bc->data.reserve.numitems = numitems; bc->data.reserve.itemsize = itemsize; - BuildBC_Common(bc); + return bc; } void -DebugPrintBC(bytecode *bc) +bytecode_print(bytecode *bc) { switch (bc->type) { case BC_EMPTY: diff --git a/libyasm/bytecode.h b/libyasm/bytecode.h index 95af9787..e4eb4c1f 100644 --- a/libyasm/bytecode.h +++ b/libyasm/bytecode.h @@ -1,4 +1,4 @@ -/* $Id: bytecode.h,v 1.18 2001/09/16 04:49:46 peter Exp $ +/* $Id: bytecode.h,v 1.19 2001/09/16 19:44:49 peter Exp $ * Bytecode utility functions header file * * Copyright (C) 2001 Peter Johnson @@ -152,39 +152,36 @@ void SetInsnLockRepPrefix(bytecode *bc, unsigned char prefix); void SetOpcodeSel(jmprel_opcode_sel *old_sel, jmprel_opcode_sel new_sel); -void BuildBC_Insn(bytecode *bc, - unsigned char opersize, - unsigned char opcode_len, - unsigned char op0, - unsigned char op1, - unsigned char op2, - effaddr *ea_ptr, - unsigned char spare, - immval *im_ptr, - unsigned char im_len, - unsigned char im_sign); - -void BuildBC_JmpRel(bytecode *bc, - targetval *target, - unsigned char short_valid, - unsigned char short_opcode_len, - unsigned char short_op0, - unsigned char short_op1, - unsigned char short_op2, - unsigned char near_valid, - unsigned char near_opcode_len, - unsigned char near_op0, - unsigned char near_op1, - unsigned char near_op2, - unsigned char addrsize); - -void BuildBC_Data(bytecode *bc, datavalhead *datahead, unsigned long size); - -void BuildBC_Reserve(bytecode *bc, - struct expr_s *numitems, - unsigned long itemsize); - -void DebugPrintBC(bytecode *bc); +bytecode *bytecode_new_insn(unsigned char opersize, + unsigned char opcode_len, + unsigned char op0, + unsigned char op1, + unsigned char op2, + effaddr *ea_ptr, + unsigned char spare, + immval *im_ptr, + unsigned char im_len, + unsigned char im_sign); + +bytecode *bytecode_new_jmprel(targetval *target, + unsigned char short_valid, + unsigned char short_opcode_len, + unsigned char short_op0, + unsigned char short_op1, + unsigned char short_op2, + unsigned char near_valid, + unsigned char near_opcode_len, + unsigned char near_op0, + unsigned char near_op1, + unsigned char near_op2, + unsigned char addrsize); + +bytecode *bytecode_new_data(datavalhead *datahead, unsigned long size); + +bytecode *bytecode_new_reserve(struct expr_s *numitems, + unsigned long itemsize); + +void bytecode_print(bytecode *bc); dataval *dataval_new_expr(struct expr_s *exp); dataval *dataval_new_float(double float_val); diff --git a/modules/parsers/nasm/bison.y.in b/modules/parsers/nasm/bison.y.in index 641b1913..574ee3b1 100644 --- a/modules/parsers/nasm/bison.y.in +++ b/modules/parsers/nasm/bison.y.in @@ -1,4 +1,4 @@ -/* $Id: bison.y.in,v 1.29 2001/09/16 18:53:47 peter Exp $ +/* $Id: bison.y.in,v 1.30 2001/09/16 19:44:49 peter Exp $ * Main bison parser * * Copyright (C) 2001 Peter Johnson, Michael Urman @@ -42,7 +42,7 @@ #include "bytecode.h" #include "section.h" -RCSID("$Id: bison.y.in,v 1.29 2001/09/16 18:53:47 peter Exp $"); +RCSID("$Id: bison.y.in,v 1.30 2001/09/16 19:44:49 peter Exp $"); #define YYDEBUG 1 @@ -53,8 +53,6 @@ void nasm_parser_error(char *); extern section *nasm_parser_cur_section; -static bytecode *new_bc; - %} %union { @@ -73,7 +71,7 @@ static bytecode *new_bc; targetval tgt_val; datavalhead datahead; dataval *data; - bytecode bc; + bytecode *bc; } %token INTNUM @@ -129,32 +127,32 @@ input: /* empty */ | input line { OutputError(); OutputWarning(); - if ($2.type != BC_EMPTY) { - new_bc = malloc(sizeof(bytecode)); - if(!new_bc) - Fatal(FATAL_NOMEM); - memcpy(new_bc, &$2, sizeof(bytecode)); - STAILQ_INSERT_TAIL(&nasm_parser_cur_section->bc, new_bc, link); + if ($2) { + if ($2->type != BC_EMPTY) { + STAILQ_INSERT_TAIL(&nasm_parser_cur_section->bc, $2, link); + } else { + free($2); + } } line_number++; } ; -line: '\n' { $$.type = BC_EMPTY; } +line: '\n' { $$ = (bytecode *)NULL; } | exp '\n' - | directive '\n' { $$.type = BC_EMPTY; } + | directive '\n' { $$ = (bytecode *)NULL; } | error '\n' { Error(_("label or instruction expected at start of line")); - $$.type = BC_EMPTY; + $$ = (bytecode *)NULL; yyerrok; } ; exp: instr - | DECLARE_DATA datavals { BuildBC_Data(&$$, &$2, $1); } - | RESERVE_SPACE expr { BuildBC_Reserve(&$$, $2, $1); } + | DECLARE_DATA datavals { $$ = bytecode_new_data(&$2, $1); } + | RESERVE_SPACE expr { $$ = bytecode_new_reserve($2, $1); } | label exp { $$ = $2; } - | label { $$.type = BC_EMPTY; } + | label { $$ = (bytecode *)NULL; } ; datavals: dataval { @@ -407,18 +405,18 @@ expr: expr_no_string explabel: ID | SPECIAL_ID | LOCAL_ID ; instr: instrbase - | OPERSIZE instr { $$ = $2; SetInsnOperSizeOverride(&$$, $1); } - | ADDRSIZE instr { $$ = $2; SetInsnAddrSizeOverride(&$$, $1); } - | REG_CS instr { $$ = $2; SetEASegment(&$$.data.insn.ea, 0x2E); } - | REG_SS instr { $$ = $2; SetEASegment(&$$.data.insn.ea, 0x36); } - | REG_DS instr { $$ = $2; SetEASegment(&$$.data.insn.ea, 0x3E); } - | REG_ES instr { $$ = $2; SetEASegment(&$$.data.insn.ea, 0x26); } - | REG_FS instr { $$ = $2; SetEASegment(&$$.data.insn.ea, 0x64); } - | REG_GS instr { $$ = $2; SetEASegment(&$$.data.insn.ea, 0x65); } - | LOCK instr { $$ = $2; SetInsnLockRepPrefix(&$$, 0xF0); } - | REPNZ instr { $$ = $2; SetInsnLockRepPrefix(&$$, 0xF2); } - | REP instr { $$ = $2; SetInsnLockRepPrefix(&$$, 0xF3); } - | REPZ instr { $$ = $2; SetInsnLockRepPrefix(&$$, 0xF4); } + | OPERSIZE instr { $$ = $2; SetInsnOperSizeOverride($$, $1); } + | ADDRSIZE instr { $$ = $2; SetInsnAddrSizeOverride($$, $1); } + | REG_CS instr { $$ = $2; SetEASegment(&$$->data.insn.ea, 0x2E); } + | REG_SS instr { $$ = $2; SetEASegment(&$$->data.insn.ea, 0x36); } + | REG_DS instr { $$ = $2; SetEASegment(&$$->data.insn.ea, 0x3E); } + | REG_ES instr { $$ = $2; SetEASegment(&$$->data.insn.ea, 0x26); } + | REG_FS instr { $$ = $2; SetEASegment(&$$->data.insn.ea, 0x64); } + | REG_GS instr { $$ = $2; SetEASegment(&$$->data.insn.ea, 0x65); } + | LOCK instr { $$ = $2; SetInsnLockRepPrefix($$, 0xF0); } + | REPNZ instr { $$ = $2; SetInsnLockRepPrefix($$, 0xF2); } + | REP instr { $$ = $2; SetInsnLockRepPrefix($$, 0xF3); } + | REPZ instr { $$ = $2; SetInsnLockRepPrefix($$, 0xF4); } ; /* instruction grammars (dynamically generated) */ diff --git a/modules/parsers/nasm/gen_instr.pl b/modules/parsers/nasm/gen_instr.pl index 1428393c..5797b514 100755 --- a/modules/parsers/nasm/gen_instr.pl +++ b/modules/parsers/nasm/gen_instr.pl @@ -1,5 +1,5 @@ #!/usr/bin/perl -w -# $Id: gen_instr.pl,v 1.19 2001/08/30 03:45:26 peter Exp $ +# $Id: gen_instr.pl,v 1.20 2001/09/16 19:44:49 peter Exp $ # Generates bison.y and token.l from instrs.dat for YASM # # Copyright (C) 2001 Michael Urman @@ -353,7 +353,7 @@ sub cond_action_if ( $ $ $ $ $ $ $ ) my ($rule, $tokens, $count, $regarg, $val, $func, $a_eax) = splice (@_); return rule_header ($rule, $tokens, $count) . <<"EOF"; if (\$$regarg == $val) { - $func(@$a_eax); + \$\$ = $func(@$a_eax); } EOF } @@ -362,7 +362,7 @@ sub cond_action_elsif ( $ $ $ $ ) my ($regarg, $val, $func, $a_eax) = splice (@_); return <<"EOF"; else if (\$$regarg == $val) { - $func(@$a_eax); + \$\$ = $func(@$a_eax); } EOF } @@ -371,7 +371,7 @@ sub cond_action_else ( $ $ ) my ($func, $a_args) = splice (@_); return <<"EOF" . rule_footer; else { - $func (@$a_args); + \$\$ = $func (@$a_args); } EOF } @@ -388,7 +388,7 @@ sub action ( @ $ ) { my ($rule, $tokens, $func, $a_args, $count) = splice @_; return rule_header ($rule, $tokens, $count) - . " $func (@$a_args);\n" + . " \$\$ = $func (@$a_args);\n" . rule_footer; } @@ -491,14 +491,11 @@ sub output_yacc ($@) if $inst->[OPERANDS] ne 'nil'; $tokens =~ s/,/ ',' /g; $tokens =~ s/:/ ':' /g; - my $func = "BuildBC_JmpRel"; + my $func = "bytecode_new_jmprel"; - # Create the argument list for BuildBC + # Create the argument list for bytecode_new my @args; - # First argument is always &$$ - push @args, '&$$,'; - # Target argument: HACK: Always assumed to be arg 1. push @args, '&$2,'; @@ -583,14 +580,11 @@ sub output_yacc ($@) $tokens =~ s/:/ ':' /g; # offset args my $to = $tokens =~ m/\b(TO|WORD|DWORD)\b/ ? 1 : 0; - my $func = "BuildBC_Insn"; + my $func = "bytecode_new_insn"; - # Create the argument list for BuildBC + # Create the argument list for bytecode_new my @args; - # First argument is always &$$ - push @args, '&$$,'; - # operand size push @args, "$inst->[OPSIZE],"; $args[-1] =~ s/nil/0/; diff --git a/modules/parsers/nasm/nasm-bison.y b/modules/parsers/nasm/nasm-bison.y index 655924e0..86ddae65 100644 --- a/modules/parsers/nasm/nasm-bison.y +++ b/modules/parsers/nasm/nasm-bison.y @@ -1,4 +1,4 @@ -/* $Id: nasm-bison.y,v 1.29 2001/09/16 18:53:47 peter Exp $ +/* $Id: nasm-bison.y,v 1.30 2001/09/16 19:44:49 peter Exp $ * Main bison parser * * Copyright (C) 2001 Peter Johnson, Michael Urman @@ -42,7 +42,7 @@ #include "bytecode.h" #include "section.h" -RCSID("$Id: nasm-bison.y,v 1.29 2001/09/16 18:53:47 peter Exp $"); +RCSID("$Id: nasm-bison.y,v 1.30 2001/09/16 19:44:49 peter Exp $"); #define YYDEBUG 1 @@ -53,8 +53,6 @@ void nasm_parser_error(char *); extern section *nasm_parser_cur_section; -static bytecode *new_bc; - %} %union { @@ -73,7 +71,7 @@ static bytecode *new_bc; targetval tgt_val; datavalhead datahead; dataval *data; - bytecode bc; + bytecode *bc; } %token INTNUM @@ -129,32 +127,32 @@ input: /* empty */ | input line { OutputError(); OutputWarning(); - if ($2.type != BC_EMPTY) { - new_bc = malloc(sizeof(bytecode)); - if(!new_bc) - Fatal(FATAL_NOMEM); - memcpy(new_bc, &$2, sizeof(bytecode)); - STAILQ_INSERT_TAIL(&nasm_parser_cur_section->bc, new_bc, link); + if ($2) { + if ($2->type != BC_EMPTY) { + STAILQ_INSERT_TAIL(&nasm_parser_cur_section->bc, $2, link); + } else { + free($2); + } } line_number++; } ; -line: '\n' { $$.type = BC_EMPTY; } +line: '\n' { $$ = (bytecode *)NULL; } | exp '\n' - | directive '\n' { $$.type = BC_EMPTY; } + | directive '\n' { $$ = (bytecode *)NULL; } | error '\n' { Error(_("label or instruction expected at start of line")); - $$.type = BC_EMPTY; + $$ = (bytecode *)NULL; yyerrok; } ; exp: instr - | DECLARE_DATA datavals { BuildBC_Data(&$$, &$2, $1); } - | RESERVE_SPACE expr { BuildBC_Reserve(&$$, $2, $1); } + | DECLARE_DATA datavals { $$ = bytecode_new_data(&$2, $1); } + | RESERVE_SPACE expr { $$ = bytecode_new_reserve($2, $1); } | label exp { $$ = $2; } - | label { $$.type = BC_EMPTY; } + | label { $$ = (bytecode *)NULL; } ; datavals: dataval { @@ -407,18 +405,18 @@ expr: expr_no_string explabel: ID | SPECIAL_ID | LOCAL_ID ; instr: instrbase - | OPERSIZE instr { $$ = $2; SetInsnOperSizeOverride(&$$, $1); } - | ADDRSIZE instr { $$ = $2; SetInsnAddrSizeOverride(&$$, $1); } - | REG_CS instr { $$ = $2; SetEASegment(&$$.data.insn.ea, 0x2E); } - | REG_SS instr { $$ = $2; SetEASegment(&$$.data.insn.ea, 0x36); } - | REG_DS instr { $$ = $2; SetEASegment(&$$.data.insn.ea, 0x3E); } - | REG_ES instr { $$ = $2; SetEASegment(&$$.data.insn.ea, 0x26); } - | REG_FS instr { $$ = $2; SetEASegment(&$$.data.insn.ea, 0x64); } - | REG_GS instr { $$ = $2; SetEASegment(&$$.data.insn.ea, 0x65); } - | LOCK instr { $$ = $2; SetInsnLockRepPrefix(&$$, 0xF0); } - | REPNZ instr { $$ = $2; SetInsnLockRepPrefix(&$$, 0xF2); } - | REP instr { $$ = $2; SetInsnLockRepPrefix(&$$, 0xF3); } - | REPZ instr { $$ = $2; SetInsnLockRepPrefix(&$$, 0xF4); } + | OPERSIZE instr { $$ = $2; SetInsnOperSizeOverride($$, $1); } + | ADDRSIZE instr { $$ = $2; SetInsnAddrSizeOverride($$, $1); } + | REG_CS instr { $$ = $2; SetEASegment(&$$->data.insn.ea, 0x2E); } + | REG_SS instr { $$ = $2; SetEASegment(&$$->data.insn.ea, 0x36); } + | REG_DS instr { $$ = $2; SetEASegment(&$$->data.insn.ea, 0x3E); } + | REG_ES instr { $$ = $2; SetEASegment(&$$->data.insn.ea, 0x26); } + | REG_FS instr { $$ = $2; SetEASegment(&$$->data.insn.ea, 0x64); } + | REG_GS instr { $$ = $2; SetEASegment(&$$->data.insn.ea, 0x65); } + | LOCK instr { $$ = $2; SetInsnLockRepPrefix($$, 0xF0); } + | REPNZ instr { $$ = $2; SetInsnLockRepPrefix($$, 0xF2); } + | REP instr { $$ = $2; SetInsnLockRepPrefix($$, 0xF3); } + | REPZ instr { $$ = $2; SetInsnLockRepPrefix($$, 0xF4); } ; /* instruction grammars (dynamically generated) */ diff --git a/src/bytecode.c b/src/bytecode.c index 2765a251..e5954562 100644 --- a/src/bytecode.c +++ b/src/bytecode.c @@ -1,4 +1,4 @@ -/* $Id: bytecode.c,v 1.20 2001/08/30 03:45:26 peter Exp $ +/* $Id: bytecode.c,v 1.21 2001/09/16 19:44:49 peter Exp $ * Bytecode utility functions * * Copyright (C) 2001 Peter Johnson @@ -41,7 +41,7 @@ #include "bytecode.h" -RCSID("$Id: bytecode.c,v 1.20 2001/08/30 03:45:26 peter Exp $"); +RCSID("$Id: bytecode.c,v 1.21 2001/09/16 19:44:49 peter Exp $"); /* Static structures for when NULL is passed to conversion functions. */ /* for Convert*ToEA() */ @@ -53,7 +53,7 @@ static immval im_static; /* for Convert*ToBytes() */ unsigned char bytes_static[16]; -static void BuildBC_Common(bytecode *bc); +static bytecode *bytecode_new_common(void); effaddr * ConvertRegToEA(effaddr *ptr, unsigned long reg) @@ -246,9 +246,14 @@ SetOpcodeSel(jmprel_opcode_sel *old_sel, jmprel_opcode_sel new_sel) *old_sel = new_sel; } -static void -BuildBC_Common(bytecode *bc) +static bytecode * +bytecode_new_common(void) { + bytecode *bc = malloc(sizeof(bytecode)); + + if (!bc) + Fatal(FATAL_NOMEM); + bc->len = 0; bc->filename = strdup(filename); @@ -258,19 +263,20 @@ BuildBC_Common(bytecode *bc) bc->mode_bits = mode_bits; } -void -BuildBC_Insn(bytecode *bc, - unsigned char opersize, - unsigned char opcode_len, - unsigned char op0, - unsigned char op1, - unsigned char op2, - effaddr *ea_ptr, - unsigned char spare, - immval *im_ptr, - unsigned char im_len, - unsigned char im_sign) +bytecode * +bytecode_new_insn(unsigned char opersize, + unsigned char opcode_len, + unsigned char op0, + unsigned char op1, + unsigned char op2, + effaddr *ea_ptr, + unsigned char spare, + immval *im_ptr, + unsigned char im_len, + unsigned char im_sign) { + bytecode *bc = bytecode_new_common(); + bc->type = BC_INSN; if (ea_ptr) { @@ -303,24 +309,25 @@ BuildBC_Insn(bytecode *bc, bc->data.insn.opersize = opersize; bc->data.insn.lockrep_pre = 0; - BuildBC_Common(bc); + return bc; } -void -BuildBC_JmpRel(bytecode *bc, - targetval *target, - unsigned char short_valid, - unsigned char short_opcode_len, - unsigned char short_op0, - unsigned char short_op1, - unsigned char short_op2, - unsigned char near_valid, - unsigned char near_opcode_len, - unsigned char near_op0, - unsigned char near_op1, - unsigned char near_op2, - unsigned char addrsize) +bytecode * +bytecode_new_jmprel(targetval *target, + unsigned char short_valid, + unsigned char short_opcode_len, + unsigned char short_op0, + unsigned char short_op1, + unsigned char short_op2, + unsigned char near_valid, + unsigned char near_opcode_len, + unsigned char near_op0, + unsigned char near_op1, + unsigned char near_op2, + unsigned char addrsize) { + bytecode *bc = bytecode_new_common(); + bc->type = BC_JMPREL; bc->data.jmprel.target = target->val; @@ -351,12 +358,13 @@ BuildBC_JmpRel(bytecode *bc, bc->data.jmprel.opersize = 0; bc->data.jmprel.lockrep_pre = 0; - BuildBC_Common(bc); + return bc; } -void -BuildBC_Data(bytecode *bc, datavalhead *datahead, unsigned long size) +bytecode * +bytecode_new_data(datavalhead *datahead, unsigned long size) { + bytecode *bc; dataval *cur; /* First check to see if all the data elements are valid for the size @@ -395,27 +403,31 @@ BuildBC_Data(bytecode *bc, datavalhead *datahead, unsigned long size) } } + bc = bytecode_new_common(); + bc->type = BC_DATA; bc->data.data.datahead = *datahead; bc->data.data.size = size; - BuildBC_Common(bc); + return bc; } -void -BuildBC_Reserve(bytecode *bc, expr *numitems, unsigned long itemsize) +bytecode * +bytecode_new_reserve(expr *numitems, unsigned long itemsize) { + bytecode *bc = bytecode_new_common(); + bc->type = BC_RESERVE; bc->data.reserve.numitems = numitems; bc->data.reserve.itemsize = itemsize; - BuildBC_Common(bc); + return bc; } void -DebugPrintBC(bytecode *bc) +bytecode_print(bytecode *bc) { switch (bc->type) { case BC_EMPTY: diff --git a/src/bytecode.h b/src/bytecode.h index 95af9787..e4eb4c1f 100644 --- a/src/bytecode.h +++ b/src/bytecode.h @@ -1,4 +1,4 @@ -/* $Id: bytecode.h,v 1.18 2001/09/16 04:49:46 peter Exp $ +/* $Id: bytecode.h,v 1.19 2001/09/16 19:44:49 peter Exp $ * Bytecode utility functions header file * * Copyright (C) 2001 Peter Johnson @@ -152,39 +152,36 @@ void SetInsnLockRepPrefix(bytecode *bc, unsigned char prefix); void SetOpcodeSel(jmprel_opcode_sel *old_sel, jmprel_opcode_sel new_sel); -void BuildBC_Insn(bytecode *bc, - unsigned char opersize, - unsigned char opcode_len, - unsigned char op0, - unsigned char op1, - unsigned char op2, - effaddr *ea_ptr, - unsigned char spare, - immval *im_ptr, - unsigned char im_len, - unsigned char im_sign); - -void BuildBC_JmpRel(bytecode *bc, - targetval *target, - unsigned char short_valid, - unsigned char short_opcode_len, - unsigned char short_op0, - unsigned char short_op1, - unsigned char short_op2, - unsigned char near_valid, - unsigned char near_opcode_len, - unsigned char near_op0, - unsigned char near_op1, - unsigned char near_op2, - unsigned char addrsize); - -void BuildBC_Data(bytecode *bc, datavalhead *datahead, unsigned long size); - -void BuildBC_Reserve(bytecode *bc, - struct expr_s *numitems, - unsigned long itemsize); - -void DebugPrintBC(bytecode *bc); +bytecode *bytecode_new_insn(unsigned char opersize, + unsigned char opcode_len, + unsigned char op0, + unsigned char op1, + unsigned char op2, + effaddr *ea_ptr, + unsigned char spare, + immval *im_ptr, + unsigned char im_len, + unsigned char im_sign); + +bytecode *bytecode_new_jmprel(targetval *target, + unsigned char short_valid, + unsigned char short_opcode_len, + unsigned char short_op0, + unsigned char short_op1, + unsigned char short_op2, + unsigned char near_valid, + unsigned char near_opcode_len, + unsigned char near_op0, + unsigned char near_op1, + unsigned char near_op2, + unsigned char addrsize); + +bytecode *bytecode_new_data(datavalhead *datahead, unsigned long size); + +bytecode *bytecode_new_reserve(struct expr_s *numitems, + unsigned long itemsize); + +void bytecode_print(bytecode *bc); dataval *dataval_new_expr(struct expr_s *exp); dataval *dataval_new_float(double float_val); diff --git a/src/parsers/nasm/bison.y.in b/src/parsers/nasm/bison.y.in index 641b1913..574ee3b1 100644 --- a/src/parsers/nasm/bison.y.in +++ b/src/parsers/nasm/bison.y.in @@ -1,4 +1,4 @@ -/* $Id: bison.y.in,v 1.29 2001/09/16 18:53:47 peter Exp $ +/* $Id: bison.y.in,v 1.30 2001/09/16 19:44:49 peter Exp $ * Main bison parser * * Copyright (C) 2001 Peter Johnson, Michael Urman @@ -42,7 +42,7 @@ #include "bytecode.h" #include "section.h" -RCSID("$Id: bison.y.in,v 1.29 2001/09/16 18:53:47 peter Exp $"); +RCSID("$Id: bison.y.in,v 1.30 2001/09/16 19:44:49 peter Exp $"); #define YYDEBUG 1 @@ -53,8 +53,6 @@ void nasm_parser_error(char *); extern section *nasm_parser_cur_section; -static bytecode *new_bc; - %} %union { @@ -73,7 +71,7 @@ static bytecode *new_bc; targetval tgt_val; datavalhead datahead; dataval *data; - bytecode bc; + bytecode *bc; } %token INTNUM @@ -129,32 +127,32 @@ input: /* empty */ | input line { OutputError(); OutputWarning(); - if ($2.type != BC_EMPTY) { - new_bc = malloc(sizeof(bytecode)); - if(!new_bc) - Fatal(FATAL_NOMEM); - memcpy(new_bc, &$2, sizeof(bytecode)); - STAILQ_INSERT_TAIL(&nasm_parser_cur_section->bc, new_bc, link); + if ($2) { + if ($2->type != BC_EMPTY) { + STAILQ_INSERT_TAIL(&nasm_parser_cur_section->bc, $2, link); + } else { + free($2); + } } line_number++; } ; -line: '\n' { $$.type = BC_EMPTY; } +line: '\n' { $$ = (bytecode *)NULL; } | exp '\n' - | directive '\n' { $$.type = BC_EMPTY; } + | directive '\n' { $$ = (bytecode *)NULL; } | error '\n' { Error(_("label or instruction expected at start of line")); - $$.type = BC_EMPTY; + $$ = (bytecode *)NULL; yyerrok; } ; exp: instr - | DECLARE_DATA datavals { BuildBC_Data(&$$, &$2, $1); } - | RESERVE_SPACE expr { BuildBC_Reserve(&$$, $2, $1); } + | DECLARE_DATA datavals { $$ = bytecode_new_data(&$2, $1); } + | RESERVE_SPACE expr { $$ = bytecode_new_reserve($2, $1); } | label exp { $$ = $2; } - | label { $$.type = BC_EMPTY; } + | label { $$ = (bytecode *)NULL; } ; datavals: dataval { @@ -407,18 +405,18 @@ expr: expr_no_string explabel: ID | SPECIAL_ID | LOCAL_ID ; instr: instrbase - | OPERSIZE instr { $$ = $2; SetInsnOperSizeOverride(&$$, $1); } - | ADDRSIZE instr { $$ = $2; SetInsnAddrSizeOverride(&$$, $1); } - | REG_CS instr { $$ = $2; SetEASegment(&$$.data.insn.ea, 0x2E); } - | REG_SS instr { $$ = $2; SetEASegment(&$$.data.insn.ea, 0x36); } - | REG_DS instr { $$ = $2; SetEASegment(&$$.data.insn.ea, 0x3E); } - | REG_ES instr { $$ = $2; SetEASegment(&$$.data.insn.ea, 0x26); } - | REG_FS instr { $$ = $2; SetEASegment(&$$.data.insn.ea, 0x64); } - | REG_GS instr { $$ = $2; SetEASegment(&$$.data.insn.ea, 0x65); } - | LOCK instr { $$ = $2; SetInsnLockRepPrefix(&$$, 0xF0); } - | REPNZ instr { $$ = $2; SetInsnLockRepPrefix(&$$, 0xF2); } - | REP instr { $$ = $2; SetInsnLockRepPrefix(&$$, 0xF3); } - | REPZ instr { $$ = $2; SetInsnLockRepPrefix(&$$, 0xF4); } + | OPERSIZE instr { $$ = $2; SetInsnOperSizeOverride($$, $1); } + | ADDRSIZE instr { $$ = $2; SetInsnAddrSizeOverride($$, $1); } + | REG_CS instr { $$ = $2; SetEASegment(&$$->data.insn.ea, 0x2E); } + | REG_SS instr { $$ = $2; SetEASegment(&$$->data.insn.ea, 0x36); } + | REG_DS instr { $$ = $2; SetEASegment(&$$->data.insn.ea, 0x3E); } + | REG_ES instr { $$ = $2; SetEASegment(&$$->data.insn.ea, 0x26); } + | REG_FS instr { $$ = $2; SetEASegment(&$$->data.insn.ea, 0x64); } + | REG_GS instr { $$ = $2; SetEASegment(&$$->data.insn.ea, 0x65); } + | LOCK instr { $$ = $2; SetInsnLockRepPrefix($$, 0xF0); } + | REPNZ instr { $$ = $2; SetInsnLockRepPrefix($$, 0xF2); } + | REP instr { $$ = $2; SetInsnLockRepPrefix($$, 0xF3); } + | REPZ instr { $$ = $2; SetInsnLockRepPrefix($$, 0xF4); } ; /* instruction grammars (dynamically generated) */ diff --git a/src/parsers/nasm/gen_instr.pl b/src/parsers/nasm/gen_instr.pl index 1428393c..5797b514 100755 --- a/src/parsers/nasm/gen_instr.pl +++ b/src/parsers/nasm/gen_instr.pl @@ -1,5 +1,5 @@ #!/usr/bin/perl -w -# $Id: gen_instr.pl,v 1.19 2001/08/30 03:45:26 peter Exp $ +# $Id: gen_instr.pl,v 1.20 2001/09/16 19:44:49 peter Exp $ # Generates bison.y and token.l from instrs.dat for YASM # # Copyright (C) 2001 Michael Urman @@ -353,7 +353,7 @@ sub cond_action_if ( $ $ $ $ $ $ $ ) my ($rule, $tokens, $count, $regarg, $val, $func, $a_eax) = splice (@_); return rule_header ($rule, $tokens, $count) . <<"EOF"; if (\$$regarg == $val) { - $func(@$a_eax); + \$\$ = $func(@$a_eax); } EOF } @@ -362,7 +362,7 @@ sub cond_action_elsif ( $ $ $ $ ) my ($regarg, $val, $func, $a_eax) = splice (@_); return <<"EOF"; else if (\$$regarg == $val) { - $func(@$a_eax); + \$\$ = $func(@$a_eax); } EOF } @@ -371,7 +371,7 @@ sub cond_action_else ( $ $ ) my ($func, $a_args) = splice (@_); return <<"EOF" . rule_footer; else { - $func (@$a_args); + \$\$ = $func (@$a_args); } EOF } @@ -388,7 +388,7 @@ sub action ( @ $ ) { my ($rule, $tokens, $func, $a_args, $count) = splice @_; return rule_header ($rule, $tokens, $count) - . " $func (@$a_args);\n" + . " \$\$ = $func (@$a_args);\n" . rule_footer; } @@ -491,14 +491,11 @@ sub output_yacc ($@) if $inst->[OPERANDS] ne 'nil'; $tokens =~ s/,/ ',' /g; $tokens =~ s/:/ ':' /g; - my $func = "BuildBC_JmpRel"; + my $func = "bytecode_new_jmprel"; - # Create the argument list for BuildBC + # Create the argument list for bytecode_new my @args; - # First argument is always &$$ - push @args, '&$$,'; - # Target argument: HACK: Always assumed to be arg 1. push @args, '&$2,'; @@ -583,14 +580,11 @@ sub output_yacc ($@) $tokens =~ s/:/ ':' /g; # offset args my $to = $tokens =~ m/\b(TO|WORD|DWORD)\b/ ? 1 : 0; - my $func = "BuildBC_Insn"; + my $func = "bytecode_new_insn"; - # Create the argument list for BuildBC + # Create the argument list for bytecode_new my @args; - # First argument is always &$$ - push @args, '&$$,'; - # operand size push @args, "$inst->[OPSIZE],"; $args[-1] =~ s/nil/0/; diff --git a/src/parsers/nasm/nasm-bison.y b/src/parsers/nasm/nasm-bison.y index 655924e0..86ddae65 100644 --- a/src/parsers/nasm/nasm-bison.y +++ b/src/parsers/nasm/nasm-bison.y @@ -1,4 +1,4 @@ -/* $Id: nasm-bison.y,v 1.29 2001/09/16 18:53:47 peter Exp $ +/* $Id: nasm-bison.y,v 1.30 2001/09/16 19:44:49 peter Exp $ * Main bison parser * * Copyright (C) 2001 Peter Johnson, Michael Urman @@ -42,7 +42,7 @@ #include "bytecode.h" #include "section.h" -RCSID("$Id: nasm-bison.y,v 1.29 2001/09/16 18:53:47 peter Exp $"); +RCSID("$Id: nasm-bison.y,v 1.30 2001/09/16 19:44:49 peter Exp $"); #define YYDEBUG 1 @@ -53,8 +53,6 @@ void nasm_parser_error(char *); extern section *nasm_parser_cur_section; -static bytecode *new_bc; - %} %union { @@ -73,7 +71,7 @@ static bytecode *new_bc; targetval tgt_val; datavalhead datahead; dataval *data; - bytecode bc; + bytecode *bc; } %token INTNUM @@ -129,32 +127,32 @@ input: /* empty */ | input line { OutputError(); OutputWarning(); - if ($2.type != BC_EMPTY) { - new_bc = malloc(sizeof(bytecode)); - if(!new_bc) - Fatal(FATAL_NOMEM); - memcpy(new_bc, &$2, sizeof(bytecode)); - STAILQ_INSERT_TAIL(&nasm_parser_cur_section->bc, new_bc, link); + if ($2) { + if ($2->type != BC_EMPTY) { + STAILQ_INSERT_TAIL(&nasm_parser_cur_section->bc, $2, link); + } else { + free($2); + } } line_number++; } ; -line: '\n' { $$.type = BC_EMPTY; } +line: '\n' { $$ = (bytecode *)NULL; } | exp '\n' - | directive '\n' { $$.type = BC_EMPTY; } + | directive '\n' { $$ = (bytecode *)NULL; } | error '\n' { Error(_("label or instruction expected at start of line")); - $$.type = BC_EMPTY; + $$ = (bytecode *)NULL; yyerrok; } ; exp: instr - | DECLARE_DATA datavals { BuildBC_Data(&$$, &$2, $1); } - | RESERVE_SPACE expr { BuildBC_Reserve(&$$, $2, $1); } + | DECLARE_DATA datavals { $$ = bytecode_new_data(&$2, $1); } + | RESERVE_SPACE expr { $$ = bytecode_new_reserve($2, $1); } | label exp { $$ = $2; } - | label { $$.type = BC_EMPTY; } + | label { $$ = (bytecode *)NULL; } ; datavals: dataval { @@ -407,18 +405,18 @@ expr: expr_no_string explabel: ID | SPECIAL_ID | LOCAL_ID ; instr: instrbase - | OPERSIZE instr { $$ = $2; SetInsnOperSizeOverride(&$$, $1); } - | ADDRSIZE instr { $$ = $2; SetInsnAddrSizeOverride(&$$, $1); } - | REG_CS instr { $$ = $2; SetEASegment(&$$.data.insn.ea, 0x2E); } - | REG_SS instr { $$ = $2; SetEASegment(&$$.data.insn.ea, 0x36); } - | REG_DS instr { $$ = $2; SetEASegment(&$$.data.insn.ea, 0x3E); } - | REG_ES instr { $$ = $2; SetEASegment(&$$.data.insn.ea, 0x26); } - | REG_FS instr { $$ = $2; SetEASegment(&$$.data.insn.ea, 0x64); } - | REG_GS instr { $$ = $2; SetEASegment(&$$.data.insn.ea, 0x65); } - | LOCK instr { $$ = $2; SetInsnLockRepPrefix(&$$, 0xF0); } - | REPNZ instr { $$ = $2; SetInsnLockRepPrefix(&$$, 0xF2); } - | REP instr { $$ = $2; SetInsnLockRepPrefix(&$$, 0xF3); } - | REPZ instr { $$ = $2; SetInsnLockRepPrefix(&$$, 0xF4); } + | OPERSIZE instr { $$ = $2; SetInsnOperSizeOverride($$, $1); } + | ADDRSIZE instr { $$ = $2; SetInsnAddrSizeOverride($$, $1); } + | REG_CS instr { $$ = $2; SetEASegment(&$$->data.insn.ea, 0x2E); } + | REG_SS instr { $$ = $2; SetEASegment(&$$->data.insn.ea, 0x36); } + | REG_DS instr { $$ = $2; SetEASegment(&$$->data.insn.ea, 0x3E); } + | REG_ES instr { $$ = $2; SetEASegment(&$$->data.insn.ea, 0x26); } + | REG_FS instr { $$ = $2; SetEASegment(&$$->data.insn.ea, 0x64); } + | REG_GS instr { $$ = $2; SetEASegment(&$$->data.insn.ea, 0x65); } + | LOCK instr { $$ = $2; SetInsnLockRepPrefix($$, 0xF0); } + | REPNZ instr { $$ = $2; SetInsnLockRepPrefix($$, 0xF2); } + | REP instr { $$ = $2; SetInsnLockRepPrefix($$, 0xF3); } + | REPZ instr { $$ = $2; SetInsnLockRepPrefix($$, 0xF4); } ; /* instruction grammars (dynamically generated) */