static asm86_t *ack_get_statement(void) /* Get a pseudo op or machine instruction with arguments. */ { token_t *t= get_token(0); asm86_t *a; mnemonic_t *m; int n; int prefix_seen; int oaz_prefix; int deref; assert(t->type == T_WORD); if (strcmp(t->name, ".sect") == 0) { /* .sect .text etc. Accept only four segment names. */ skip_token(1); t= get_token(0); if (t->type != T_WORD || ( strcmp(t->name, ".text") != 0 && strcmp(t->name, ".rom") != 0 && strcmp(t->name, ".data") != 0 && strcmp(t->name, ".bss") != 0 && strcmp(t->name, ".end") != 0 )) { parse_err(1, t, "weird section name to .sect\n"); return nil; } } a= new_asm86(); /* Process instruction prefixes. */ oaz_prefix= 0; for (prefix_seen= 0;; prefix_seen= 1) { if (strcmp(t->name, "o16") == 0) { if (use16()) { parse_err(1, t, "o16 in an 8086 section\n"); } oaz_prefix|= OPZ; } else if (strcmp(t->name, "o32") == 0) { if (use32()) { parse_err(1, t, "o32 in an 80386 section\n"); } oaz_prefix|= OPZ; } else if (strcmp(t->name, "a16") == 0) { if (use16()) { parse_err(1, t, "a16 in an 8086 section\n"); } oaz_prefix|= ADZ; } else if (strcmp(t->name, "a32") == 0) { if (use32()) { parse_err(1, t, "a32 in an 80386 section\n"); } oaz_prefix|= ADZ; } else if (strcmp(t->name, "rep") == 0 || strcmp(t->name, "repe") == 0 || strcmp(t->name, "repne") == 0 || strcmp(t->name, "repz") == 0 || strcmp(t->name, "repnz") == 0 ) { if (a->rep != ONCE) { parse_err(1, t, "can't have more than one rep\n"); } switch (t->name[3]) { case 0: a->rep= REP; break; case 'e': case 'z': a->rep= REPE; break; case 'n': a->rep= REPNE; break; } } else if (strchr("cdefgs", t->name[0]) != nil && strcmp(t->name+1, "seg") == 0) { if (a->seg != DEFSEG) { parse_err(1, t, "can't have more than one segment prefix\n"); } switch (t->name[0]) { case 'c': a->seg= CSEG; break; case 'd': a->seg= DSEG; break; case 'e': a->seg= ESEG; break; case 'f': a->seg= FSEG; break; case 'g': a->seg= GSEG; break; case 's': a->seg= SSEG; break; } } else if (!prefix_seen) { /* No prefix here, get out! */ break; } else { /* No more prefixes, next must be an instruction. */ if (t->type != T_WORD || (m= search_mnem(t->name)) == nil || m->optype == PSEUDO ) { parse_err(1, t, "machine instruction expected after instruction prefix\n"); del_asm86(a); return nil; } if (oaz_prefix != 0 && m->optype != JUMP && m->optype != WORD) { parse_err(1, t, "'%s' can't have an operand size prefix\n", m->name); } break; } /* Skip the prefix and extra newlines. */ do { skip_token(1); } while ((t= get_token(0))->symbol == ';'); } /* All the readahead being done upsets the line counter. */ a->line= t->line; /* Read a machine instruction or pseudo op. */ if ((m= search_mnem(t->name)) == nil) { parse_err(1, t, "unknown instruction '%s'\n", t->name); del_asm86(a); return nil; } a->opcode= m->opcode; a->optype= m->optype; a->oaz= oaz_prefix; switch (a->opcode) { case IN: case OUT: case INT: deref= 0; break; default: deref= (a->optype >= BYTE); } n= 1; if (get_token(1)->symbol != ';' && (a->args= ack_get_oplist(&n, deref)) == nil) { del_asm86(a); return nil; } if (get_token(n)->symbol != ';') { parse_err(1, t, "garbage at end of instruction\n"); del_asm86(a); return nil; } switch (a->opcode) { case DOT_ALIGN: /* Restrict .align to have a single numeric argument, some * assemblers think of the argument as a power of two, so * we need to be able to change the value. */ if (a->args == nil || a->args->operator != 'W' || !isanumber(a->args->name)) { parse_err(1, t, ".align is restricted to one numeric argument\n"); del_asm86(a); return nil; } break; case JMPF: case CALLF: /* NCC jmpf off,seg -> ACK jmpf seg:off */ if (dialect == NCC && a->args != nil && a->args->operator == ',') { expression_t *t; t= a->args->left; a->args->left= a->args->right; a->args->right= t; break; } /*FALL THROUGH*/ case JMP: case CALL: /* NCC jmp @(reg) -> ACK jmp (reg) */ if (dialect == NCC && a->args != nil && ( (a->args->operator == '(' && a->args->middle != nil && a->args->middle->operator == 'O') || (a->args->operator == 'O' && a->args->left == nil && a->args->middle != nil && a->args->right == nil) )) { expression_t *t; t= a->args; a->args= a->args->middle; t->middle= nil; del_expr(t); if (a->args->operator == 'B') a->args->operator= 'W'; } break; default:; } skip_token(n+1); return a; }
static expression_t *ack_get_operand(asm86_t * a, int *pn, int deref) /* Get something like: (memory), offset(base)(index*scale), or simpler. */ { expression_t *e, *offset, *base, *index; token_t *t; int c; /* Is it (memory)? */ if (get_token(*pn)->symbol == '(' && ((t= get_token(*pn + 1))->type != T_WORD || !test_register(t->name, a)) ) { /* A memory dereference. */ (*pn)++; if ((offset= ack_get_C_expression(pn, a)) == nil) return nil; if (get_token(*pn)->symbol != ')') { parse_err(1, t, "operand syntax error\n"); del_expr(offset); return nil; } (*pn)++; e= new_expr(); e->operator= '('; e->middle= offset; /* * the default size is WORD however int on i386 is 32bit and the * assembler cannot figure it out on its own. We are also taking * our chance here though */ if (use32() && a->optype == WORD) a->optype = OWORD; return e; } /* #constant? */ if (dialect == NCC && deref && ((c= get_token(*pn)->symbol) == '#' || c == '*')) { /* NCC: mov ax,#constant -> ACK: mov ax,constant */ (*pn)++; return ack_get_C_expression(pn, a); } /* @address? */ if (dialect == NCC && get_token(*pn)->symbol == '@') { /* NCC: jmp @address -> ACK: jmp (address) */ (*pn)++; if ((offset= ack_get_operand(a, pn, deref)) == nil) return nil; e= new_expr(); e->operator= '('; e->middle= offset; return e; } /* Offset? */ if (get_token(*pn)->symbol != '(') { /* There is an offset. */ if ((offset= ack_get_C_expression(pn, a)) == nil) return nil; } else { /* No offset. */ offset= nil; } /* (base)? */ if (get_token(*pn)->symbol == '(' && (t= get_token(*pn + 1))->type == T_WORD && test_register(t->name, a) && get_token(*pn + 2)->symbol == ')' ) { /* A base register expression. */ base= new_expr(); base->operator= 'B'; base->name= copystr(t->name); (*pn)+= 3; } else { /* No base register expression. */ base= nil; } /* (index*scale)? */ if (get_token(*pn)->symbol == '(') { /* An index most likely. */ token_t *m= nil; if (!( /* This must be true: */ (t= get_token(*pn + 1))->type == T_WORD && test_register(t->name, a) && (get_token(*pn + 2)->symbol == ')' || ( get_token(*pn + 2)->symbol == '*' && (m= get_token(*pn + 3))->type == T_WORD && strchr("1248", m->name[0]) != nil && m->name[1] == 0 && get_token(*pn + 4)->symbol == ')' )) )) { /* Alas it isn't */ parse_err(1, t, "operand syntax error\n"); del_expr(offset); del_expr(base); return nil; } /* Found an index. */ index= new_expr(); index->operator= m == nil ? '1' : m->name[0]; index->name= copystr(t->name); (*pn)+= (m == nil ? 3 : 5); } else { /* No index. */ index= nil; } if (dialect == NCC && deref && base == nil && index == nil && !(offset != nil && offset->operator == 'W' && test_register(offset->name, a)) ) { /* NCC: mov ax,thing -> ACK mov ax,(thing) */ e= new_expr(); e->operator= '('; e->middle= offset; return e; } if (base == nil && index == nil) { /* Return a lone offset as is. */ e= offset; } else { e= new_expr(); e->operator= 'O'; e->left= offset; e->middle= base; e->right= index; } return e; }