From ae85d1edb123ed6f2b914b0da1473ae7c8f7c5ea Mon Sep 17 00:00:00 2001 From: Peter Johnson Date: Mon, 3 Dec 2001 07:33:48 +0000 Subject: [PATCH] Add support for incbin pseudo-instruction. svn path=/trunk/yasm/; revision=386 --- libyasm/bytecode.c | 48 +++++++++++++++++++++++++++++++ libyasm/bytecode.h | 8 ++++-- modules/parsers/nasm/bison.y.in | 7 +++-- modules/parsers/nasm/nasm-bison.y | 7 +++-- src/bytecode.c | 48 +++++++++++++++++++++++++++++++ src/bytecode.h | 8 ++++-- src/parsers/nasm/bison.y.in | 7 +++-- src/parsers/nasm/nasm-bison.y | 7 +++-- 8 files changed, 128 insertions(+), 12 deletions(-) diff --git a/libyasm/bytecode.c b/libyasm/bytecode.c index fe9f7006..93289c6f 100644 --- a/libyasm/bytecode.c +++ b/libyasm/bytecode.c @@ -58,6 +58,16 @@ typedef struct bytecode_reserve { unsigned char itemsize; /* size of each item (in bytes) */ } bytecode_reserve; +typedef struct bytecode_incbin { + /*@only@*/ char *filename; /* file to include data from */ + + /* starting offset to read from (NULL=0) */ + /*@only@*/ /*@null@*/ expr *start; + + /* maximum number of bytes to read (NULL=no limit) */ + /*@only@*/ /*@null@*/ expr *maxlen; +} bytecode_incbin; + /* Static structures for when NULL is passed to conversion functions. */ /* for Convert*ToBytes() */ unsigned char bytes_static[16]; @@ -174,11 +184,25 @@ bc_new_reserve(expr *numitems, unsigned char itemsize) return bc; } +bytecode * +bc_new_incbin(char *filename, expr *start, expr *maxlen) +{ + bytecode *bc = bc_new_common(BC_INCBIN, sizeof(bytecode_incbin)); + bytecode_incbin *incbin = bc_get_data(bc); + + incbin->filename = filename; + incbin->start = start; + incbin->maxlen = maxlen; + + return bc; +} + void bc_delete(bytecode *bc) { bytecode_data *data; bytecode_reserve *reserve; + bytecode_incbin *incbin; if (!bc) return; @@ -194,6 +218,12 @@ bc_delete(bytecode *bc) reserve = bc_get_data(bc); expr_delete(reserve->numitems); break; + case BC_INCBIN: + incbin = bc_get_data(bc); + xfree(incbin->filename); + expr_delete(incbin->start); + expr_delete(incbin->maxlen); + break; default: if (bc->type < cur_arch->bc.type_max) cur_arch->bc.bc_delete(bc); @@ -218,6 +248,7 @@ bc_print(FILE *f, const bytecode *bc) { const bytecode_data *data; const bytecode_reserve *reserve; + const bytecode_incbin *incbin; switch (bc->type) { case BC_EMPTY: @@ -242,6 +273,23 @@ bc_print(FILE *f, const bytecode *bc) fprintf(f, "\n%*sItem Size=%u\n", indent_level, "", (unsigned int)reserve->itemsize); break; + case BC_INCBIN: + incbin = bc_get_const_data(bc); + fprintf(f, "%*s_IncBin_\n", indent_level, ""); + fprintf(f, "%*sFilename=`%s'\n", indent_level, "", + incbin->filename); + fprintf(f, "%*sStart=", indent_level, ""); + if (!incbin->start) + fprintf(f, "nil (0)"); + else + expr_print(f, incbin->start); + fprintf(f, "%*sMax Len=", indent_level, ""); + if (!incbin->maxlen) + fprintf(f, "nil (unlimited)"); + else + expr_print(f, incbin->maxlen); + fprintf(f, "\n"); + break; default: if (bc->type < cur_arch->bc.type_max) cur_arch->bc.bc_print(f, bc); diff --git a/libyasm/bytecode.h b/libyasm/bytecode.h index 60156416..523a4b9b 100644 --- a/libyasm/bytecode.h +++ b/libyasm/bytecode.h @@ -33,9 +33,10 @@ typedef struct dataval dataval; typedef enum { BC_EMPTY = 0, BC_DATA, - BC_RESERVE + BC_RESERVE, + BC_INCBIN } bytecode_type; -#define BYTECODE_TYPE_BASE BC_RESERVE+1 +#define BYTECODE_TYPE_BASE BC_INCBIN+1 /*@only@*/ immval *imm_new_int(unsigned long int_val); /*@only@*/ immval *imm_new_expr(/*@keep@*/ expr *e); @@ -49,6 +50,9 @@ void bc_set_multiple(bytecode *bc, /*@keep@*/ expr *e); /*@only@*/ bytecode *bc_new_data(datavalhead *datahead, unsigned char size); /*@only@*/ bytecode *bc_new_reserve(/*@keep@*/ expr *numitems, unsigned char itemsize); +/*@only@*/ bytecode *bc_new_incbin(/*@only@*/ char *filename, + /*@only@*/ /*@null@*/ expr *start, + /*@only@*/ /*@null@*/ expr *maxlen); void bc_delete(/*@only@*/ /*@null@*/ bytecode *bc); diff --git a/modules/parsers/nasm/bison.y.in b/modules/parsers/nasm/bison.y.in index 729a9633..87d4c67a 100644 --- a/modules/parsers/nasm/bison.y.in +++ b/modules/parsers/nasm/bison.y.in @@ -181,8 +181,11 @@ lineexp: exp ; exp: instr - | DECLARE_DATA datavals { $$ = bc_new_data(&$2, $1); } - | RESERVE_SPACE expr { $$ = bc_new_reserve($2, $1); } + | DECLARE_DATA datavals { $$ = bc_new_data(&$2, $1); } + | RESERVE_SPACE expr { $$ = bc_new_reserve($2, $1); } + | INCBIN STRING { $$ = bc_new_incbin($2, NULL, NULL); } + | INCBIN STRING ',' expr { $$ = bc_new_incbin($2, $4, NULL); } + | INCBIN STRING ',' expr ',' expr { $$ = bc_new_incbin($2, $4, $6); } ; datavals: dataval { dvs_initialize(&$$); dvs_append(&$$, $1); } diff --git a/modules/parsers/nasm/nasm-bison.y b/modules/parsers/nasm/nasm-bison.y index 729a9633..87d4c67a 100644 --- a/modules/parsers/nasm/nasm-bison.y +++ b/modules/parsers/nasm/nasm-bison.y @@ -181,8 +181,11 @@ lineexp: exp ; exp: instr - | DECLARE_DATA datavals { $$ = bc_new_data(&$2, $1); } - | RESERVE_SPACE expr { $$ = bc_new_reserve($2, $1); } + | DECLARE_DATA datavals { $$ = bc_new_data(&$2, $1); } + | RESERVE_SPACE expr { $$ = bc_new_reserve($2, $1); } + | INCBIN STRING { $$ = bc_new_incbin($2, NULL, NULL); } + | INCBIN STRING ',' expr { $$ = bc_new_incbin($2, $4, NULL); } + | INCBIN STRING ',' expr ',' expr { $$ = bc_new_incbin($2, $4, $6); } ; datavals: dataval { dvs_initialize(&$$); dvs_append(&$$, $1); } diff --git a/src/bytecode.c b/src/bytecode.c index fe9f7006..93289c6f 100644 --- a/src/bytecode.c +++ b/src/bytecode.c @@ -58,6 +58,16 @@ typedef struct bytecode_reserve { unsigned char itemsize; /* size of each item (in bytes) */ } bytecode_reserve; +typedef struct bytecode_incbin { + /*@only@*/ char *filename; /* file to include data from */ + + /* starting offset to read from (NULL=0) */ + /*@only@*/ /*@null@*/ expr *start; + + /* maximum number of bytes to read (NULL=no limit) */ + /*@only@*/ /*@null@*/ expr *maxlen; +} bytecode_incbin; + /* Static structures for when NULL is passed to conversion functions. */ /* for Convert*ToBytes() */ unsigned char bytes_static[16]; @@ -174,11 +184,25 @@ bc_new_reserve(expr *numitems, unsigned char itemsize) return bc; } +bytecode * +bc_new_incbin(char *filename, expr *start, expr *maxlen) +{ + bytecode *bc = bc_new_common(BC_INCBIN, sizeof(bytecode_incbin)); + bytecode_incbin *incbin = bc_get_data(bc); + + incbin->filename = filename; + incbin->start = start; + incbin->maxlen = maxlen; + + return bc; +} + void bc_delete(bytecode *bc) { bytecode_data *data; bytecode_reserve *reserve; + bytecode_incbin *incbin; if (!bc) return; @@ -194,6 +218,12 @@ bc_delete(bytecode *bc) reserve = bc_get_data(bc); expr_delete(reserve->numitems); break; + case BC_INCBIN: + incbin = bc_get_data(bc); + xfree(incbin->filename); + expr_delete(incbin->start); + expr_delete(incbin->maxlen); + break; default: if (bc->type < cur_arch->bc.type_max) cur_arch->bc.bc_delete(bc); @@ -218,6 +248,7 @@ bc_print(FILE *f, const bytecode *bc) { const bytecode_data *data; const bytecode_reserve *reserve; + const bytecode_incbin *incbin; switch (bc->type) { case BC_EMPTY: @@ -242,6 +273,23 @@ bc_print(FILE *f, const bytecode *bc) fprintf(f, "\n%*sItem Size=%u\n", indent_level, "", (unsigned int)reserve->itemsize); break; + case BC_INCBIN: + incbin = bc_get_const_data(bc); + fprintf(f, "%*s_IncBin_\n", indent_level, ""); + fprintf(f, "%*sFilename=`%s'\n", indent_level, "", + incbin->filename); + fprintf(f, "%*sStart=", indent_level, ""); + if (!incbin->start) + fprintf(f, "nil (0)"); + else + expr_print(f, incbin->start); + fprintf(f, "%*sMax Len=", indent_level, ""); + if (!incbin->maxlen) + fprintf(f, "nil (unlimited)"); + else + expr_print(f, incbin->maxlen); + fprintf(f, "\n"); + break; default: if (bc->type < cur_arch->bc.type_max) cur_arch->bc.bc_print(f, bc); diff --git a/src/bytecode.h b/src/bytecode.h index 60156416..523a4b9b 100644 --- a/src/bytecode.h +++ b/src/bytecode.h @@ -33,9 +33,10 @@ typedef struct dataval dataval; typedef enum { BC_EMPTY = 0, BC_DATA, - BC_RESERVE + BC_RESERVE, + BC_INCBIN } bytecode_type; -#define BYTECODE_TYPE_BASE BC_RESERVE+1 +#define BYTECODE_TYPE_BASE BC_INCBIN+1 /*@only@*/ immval *imm_new_int(unsigned long int_val); /*@only@*/ immval *imm_new_expr(/*@keep@*/ expr *e); @@ -49,6 +50,9 @@ void bc_set_multiple(bytecode *bc, /*@keep@*/ expr *e); /*@only@*/ bytecode *bc_new_data(datavalhead *datahead, unsigned char size); /*@only@*/ bytecode *bc_new_reserve(/*@keep@*/ expr *numitems, unsigned char itemsize); +/*@only@*/ bytecode *bc_new_incbin(/*@only@*/ char *filename, + /*@only@*/ /*@null@*/ expr *start, + /*@only@*/ /*@null@*/ expr *maxlen); void bc_delete(/*@only@*/ /*@null@*/ bytecode *bc); diff --git a/src/parsers/nasm/bison.y.in b/src/parsers/nasm/bison.y.in index 729a9633..87d4c67a 100644 --- a/src/parsers/nasm/bison.y.in +++ b/src/parsers/nasm/bison.y.in @@ -181,8 +181,11 @@ lineexp: exp ; exp: instr - | DECLARE_DATA datavals { $$ = bc_new_data(&$2, $1); } - | RESERVE_SPACE expr { $$ = bc_new_reserve($2, $1); } + | DECLARE_DATA datavals { $$ = bc_new_data(&$2, $1); } + | RESERVE_SPACE expr { $$ = bc_new_reserve($2, $1); } + | INCBIN STRING { $$ = bc_new_incbin($2, NULL, NULL); } + | INCBIN STRING ',' expr { $$ = bc_new_incbin($2, $4, NULL); } + | INCBIN STRING ',' expr ',' expr { $$ = bc_new_incbin($2, $4, $6); } ; datavals: dataval { dvs_initialize(&$$); dvs_append(&$$, $1); } diff --git a/src/parsers/nasm/nasm-bison.y b/src/parsers/nasm/nasm-bison.y index 729a9633..87d4c67a 100644 --- a/src/parsers/nasm/nasm-bison.y +++ b/src/parsers/nasm/nasm-bison.y @@ -181,8 +181,11 @@ lineexp: exp ; exp: instr - | DECLARE_DATA datavals { $$ = bc_new_data(&$2, $1); } - | RESERVE_SPACE expr { $$ = bc_new_reserve($2, $1); } + | DECLARE_DATA datavals { $$ = bc_new_data(&$2, $1); } + | RESERVE_SPACE expr { $$ = bc_new_reserve($2, $1); } + | INCBIN STRING { $$ = bc_new_incbin($2, NULL, NULL); } + | INCBIN STRING ',' expr { $$ = bc_new_incbin($2, $4, NULL); } + | INCBIN STRING ',' expr ',' expr { $$ = bc_new_incbin($2, $4, $6); } ; datavals: dataval { dvs_initialize(&$$); dvs_append(&$$, $1); } -- 2.50.0