0
0
mirror of https://github.com/netwide-assembler/nasm.git synced 2025-09-22 10:43:39 -04:00

nasmlib: Introduce idata_bytes helper

This allow us to eliminate code duplication

Signed-off-by: Cyrill Gorcunov <gorcunov@gmail.com>
This commit is contained in:
Cyrill Gorcunov
2009-10-31 20:02:14 +03:00
parent 41208028ff
commit bafd877d48
4 changed files with 63 additions and 90 deletions

View File

@@ -344,40 +344,16 @@ int64_t assemble(int32_t segment, int64_t offset, int bits, uint32_t cp,
int64_t insn_end; int64_t insn_end;
int32_t itimes; int32_t itimes;
int64_t start = offset; int64_t start = offset;
int64_t wsize = 0; /* size for DB etc. */ int64_t wsize; /* size for DB etc. */
errfunc = error; /* to pass to other functions */ errfunc = error; /* to pass to other functions */
cpu = cp; cpu = cp;
outfmt = output; /* likewise */ outfmt = output; /* likewise */
list = listgen; /* and again */ list = listgen; /* and again */
switch (instruction->opcode) { wsize = idata_bytes(instruction->opcode);
case -1: if (wsize == -1)
return 0; return 0;
case I_DB:
wsize = 1;
break;
case I_DW:
wsize = 2;
break;
case I_DD:
wsize = 4;
break;
case I_DQ:
wsize = 8;
break;
case I_DT:
wsize = 10;
break;
case I_DO:
wsize = 16;
break;
case I_DY:
wsize = 32;
break;
default:
break;
}
if (wsize) { if (wsize) {
extop *e; extop *e;
@@ -684,34 +660,10 @@ int64_t insn_size(int32_t segment, int64_t offset, int bits, uint32_t cp,
instruction->opcode == I_DT || instruction->opcode == I_DO || instruction->opcode == I_DT || instruction->opcode == I_DO ||
instruction->opcode == I_DY) { instruction->opcode == I_DY) {
extop *e; extop *e;
int32_t isize, osize, wsize = 0; /* placate gcc */ int32_t isize, osize, wsize;
isize = 0; isize = 0;
switch (instruction->opcode) { wsize = idata_bytes(instruction->opcode);
case I_DB:
wsize = 1;
break;
case I_DW:
wsize = 2;
break;
case I_DD:
wsize = 4;
break;
case I_DQ:
wsize = 8;
break;
case I_DT:
wsize = 10;
break;
case I_DO:
wsize = 16;
break;
case I_DY:
wsize = 32;
break;
default:
break;
}
list_for_each(e, instruction->eops) { list_for_each(e, instruction->eops) {
int32_t align; int32_t align;

View File

@@ -689,3 +689,41 @@ char *nasm_zap_spaces_rev(char *p)
*p-- = 0x0; *p-- = 0x0;
return p; return p;
} }
/*
* initialized data bytes length from opcode
*/
int idata_bytes(int opcode)
{
int ret;
switch (opcode) {
case I_DB:
ret = 1;
break;
case I_DW:
ret = 2;
break;
case I_DD:
ret = 4;
break;
case I_DQ:
ret = 8;
break;
case I_DT:
ret = 10;
break;
case I_DO:
ret = 16;
break;
case I_DY:
ret = 32;
break;
case I_none:
ret = -1;
break;
default:
ret = 0;
break;
}
return ret;
}

View File

@@ -415,4 +415,6 @@ static inline bool overflow_unsigned(int64_t value, int bytes)
return value < vmin || value > vmax; return value < vmin || value > vmax;
} }
int idata_bytes(int opcode);
#endif #endif

View File

@@ -429,44 +429,25 @@ restart_parse:
goto is_float; goto is_float;
} }
} else if (i == TOKEN_FLOAT) { } else if (i == TOKEN_FLOAT) {
is_float: is_float:
eop->type = EOT_DB_STRING; eop->type = EOT_DB_STRING;
result->eops_float = true; result->eops_float = true;
switch (result->opcode) {
case I_DB: eop->stringlen = idata_bytes(result->opcode);
eop->stringlen = 1; if (eop->stringlen > 16) {
break; nasm_error(ERR_NONFATAL, "floating-point constant"
case I_DW: " encountered in DY instruction");
eop->stringlen = 2; eop->stringlen = 0;
break; } else if (eop->stringlen < 1) {
case I_DD: nasm_error(ERR_NONFATAL, "floating-point constant"
eop->stringlen = 4; " encountered in unknown instruction");
break; /*
case I_DQ: * fix suggested by Pedro Gimeno... original line was:
eop->stringlen = 8; * eop->type = EOT_NOTHING;
break; */
case I_DT: eop->stringlen = 0;
eop->stringlen = 10; }
break;
case I_DO:
eop->stringlen = 16;
break;
case I_DY:
nasm_error(ERR_NONFATAL, "floating-point constant"
" encountered in DY instruction");
eop->stringlen = 0;
break;
default:
nasm_error(ERR_NONFATAL, "floating-point constant"
" encountered in unknown instruction");
/*
* fix suggested by Pedro Gimeno... original line
* was:
* eop->type = EOT_NOTHING;
*/
eop->stringlen = 0;
break;
}
eop = nasm_realloc(eop, sizeof(extop) + eop->stringlen); eop = nasm_realloc(eop, sizeof(extop) + eop->stringlen);
tail = &eop->next; tail = &eop->next;
*fixptr = eop; *fixptr = eop;
@@ -481,7 +462,7 @@ restart_parse:
/* anything else, assume it is an expression */ /* anything else, assume it is an expression */
expr *value; expr *value;
is_expression: is_expression:
value = evaluate(stdscan, NULL, &tokval, NULL, value = evaluate(stdscan, NULL, &tokval, NULL,
critical, nasm_error, NULL); critical, nasm_error, NULL);
i = tokval.t_type; i = tokval.t_type;