Mercurial > hg-old > index.cgi
view src/pseudo.c @ 52:b9856da2674a
Added file inclusion
author | lost |
---|---|
date | Sun, 04 Jan 2009 20:16:38 +0000 |
parents | e672232caffe |
children | 493cb8ea50a0 |
line wrap: on
line source
/* pseudo.c Copyright © 2009 William Astle This file is part of LWASM. LWASM is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>. This file implements the various pseudo operations. */ #include <stdlib.h> #include <string.h> #include "lwasm.h" #include "instab.h" #include "expr.h" #include "util.h" extern int lwasm_read_file(asmstate_t *as, const char *filename); OPFUNC(pseudo_org) { int rval; lwasm_expr_stack_t *s; if (as -> passnum != 1) { // org is not needed to be processed on pass 2 // this will prevent phasing errors for forward references that // resolve on the second pass // we saved the org address in l -> codeaddr on pass 1 as -> addr = l -> codeaddr; return; } if (l -> sym) { register_error(as, l, 1, "No symbol allowed with ORG"); } s = lwasm_evaluate_expr(as, l, *p, NULL); if (!s) { register_error(as, l, 1, "Bad expression"); return; } if (!lwasm_expr_is_constant(s)) { register_error(as, l, 1, "Illegal incomplete reference (pass 1)"); lwasm_expr_stack_free(s); return; } rval = lwasm_expr_get_value(s) & 0xffff; lwasm_expr_stack_free(s); l -> codeaddr = rval; l -> addrset = 1; as -> addr = rval; } /* The operand for include is a string optionally enclosed in " */ OPFUNC(pseudo_include) { int v1; char *fn; // only include files on pass 1 // but make sure local include context is right // for the next line... if (as -> passnum != 1) { as -> context += 1; return; } while (**p && isspace(**p)) (*p)++; if (!**p) { register_error(as, l, 1, "Bad file name"); return; } if (**p == '"') { // search for ending " (*p)++; for (v1 = 0; *((*p)+v1) && *((*p)+v1) != '"'; v1++) /* do nothing */ ; if (*((*p)+v1) != '"') { register_error(as, l, 1, "Bad file name"); return; } } else { // search for a space type character for (v1 = 0; *((*p)+v1) && !isspace(*((*p)+v1)); v1++) ; } fn = lwasm_alloc(v1 + 1); memcpy(fn, *p, v1); fn[v1] = '\0'; // end local label context on include as -> context += 1; if (lwasm_read_file(as, fn) < 0) { register_error(as, l, 1, "File include error (%s)", fn); } lwasm_free(fn); } OPFUNC(pseudo_rmb) { int rval; lwasm_expr_stack_t *s; s = lwasm_evaluate_expr(as, l, *p, NULL); if (!s) { register_error(as, l, 1, "Bad expression"); return; } if (!lwasm_expr_is_constant(s)) { register_error(as, l, 1, "Illegal incomplete reference (pass 1)"); lwasm_expr_stack_free(s); return; } rval = lwasm_expr_get_value(s); lwasm_expr_stack_free(s); l -> nocodelen = rval; as -> addr += rval; } /* void pseudo_rmd(asmstate_t *as, sourceline_t *cl, char **optr) { int rval, v1; rval = eval_expr(as, cl, optr, &v1); if (rval < 0) { errorp1(ERR_FORWARD); return; } if (v1 < 0) { errorp1(ERR_BADOPER); return; } cl -> len = v1 * 2; cl -> nocode = 1; } void pseudo_rmq(asmstate_t *as, sourceline_t *cl, char **optr) { int rval, v1; rval = eval_expr(as, cl, optr, &v1); if (rval < 0) { errorp1(ERR_FORWARD); return; } if (v1 < 0) { errorp1(ERR_BADOPER); return; } cl -> len = v1 * 4; cl -> nocode = 1; } void pseudo_zmb(asmstate_t *as, sourceline_t *cl, char **optr) { int rval, v1; rval = eval_expr(as, cl, optr, &v1); if (rval < 0) { errorp1(ERR_FORWARD); return; } if (v1 < 0) { errorp1(ERR_BADOPER); return; } while (v1--) emit(0); } void pseudo_zmd(asmstate_t *as, sourceline_t *cl, char **optr) { int rval, v1; rval = eval_expr(as, cl, optr, &v1); if (rval < 0) { errorp1(ERR_FORWARD); return; } if (v1 < 0) { errorp1(ERR_BADOPER); return; } while (v1--) { emit(0); emit(0); } } void pseudo_zmq(asmstate_t *as, sourceline_t *cl, char **optr) { int rval, v1; rval = eval_expr(as, cl, optr, &v1); if (rval < 0) { errorp1(ERR_FORWARD); return; } if (v1 < 0) { errorp1(ERR_BADOPER); return; } while (v1--) { emit(0); emit(0); emit(0); emit(0); } } void pseudo_end(asmstate_t *as, sourceline_t *cl, char **optr) { int rval, v1; while (**optr && isspace(**optr)) ; if (**optr && **optr != '*' && **optr != ';') { rval = eval_expr(as, cl, optr, &v1); if (rval < 0) { errorp1(ERR_FORWARD); return; } } else { v1 = 0; } if (as -> passnum == 2) as -> execaddr = v1; } void pseudo_align(asmstate_t *as, sourceline_t *cl, char **optr) { int rval, v1; int cn; rval = eval_expr(as, cl, optr, &v1); if (rval < 0) { errorp1(ERR_FORWARD); return; } cn = cl -> addr % v1; if (cn) cn = v1 - cn; while (cn) { emit(0); cn--; } } */ OPFUNC(pseudo_equ) { lwasm_expr_stack_t *s; int rval; // equ is not needed to be processed on pass 2 if (as -> passnum != 1) return; if (l -> sym == NULL) { register_error(as, l, 1, "No symbol specified"); return; } s = lwasm_evaluate_expr(as, l, *p, NULL); if (!s) { register_error(as, l, 1, "Bad expression"); rval = 0; } else { if (!lwasm_expr_is_constant(s)) register_error(as, l, 1, "Invalid incomplete reference (pass 1)"); rval = lwasm_expr_get_value(s); lwasm_expr_stack_free(s); } l -> symaddr = rval & 0xFFFF; l -> addrset = 2; if (strchr(l -> sym, '@') || strchr(l -> sym, '?')) lwasm_set_symbol(as, l -> sym, as -> context, l -> symaddr); else lwasm_set_symbol(as, l -> sym, -1, l -> symaddr); } /* void pseudo_set(asmstate_t *as, sourceline_t *cl, char **optr) { int rval, v1; if (cl -> hassym == 0) { errorp1(ERR_NOSYM); return; } rval = eval_expr(as, cl, optr, &v1); // eval_expr returns -1 if there was a forward ref // or -2 if the expr was invalid if (rval == -2) { // carp errorp1(ERR_FORWARD); } if (rval < 0) { // remove symbol ref cl -> hassym = 0; // mark as a "comment" so it isn't processed next time cl -> opcode = -1; return; } cl -> code_symloc = v1; cl -> isset = 1; cl -> isequ = 1; cl -> symaddr = v1 & 0xFFFF; } void pseudo_setdp(asmstate_t *as, sourceline_t *cl, char **optr) { int rval, v1; if (cl -> hassym) { register_error(as, cl, ERR_SYM); cl -> hassym = 0; return; } else { rval = eval_expr(as, cl, optr, &v1); if (rval == -1) { errorp1(ERR_FORWARD); } if (rval < 0) { cl -> opcode = -1; return; } } // setdp needs to resolve properly on pass 2 if (v1 > 0xff || v1 < 0) { errorp1(ERR_OVERFLOW); } as -> dpval = v1 & 0xff; cl -> dpval = v1 & 0xff; cl -> issetdp = 1; cl -> numcodebytes = 0; //printf("%s\n", "SETDP2"); } void pseudo_fcc(asmstate_t *as, sourceline_t *cl, char **optr) { int cn = 0; int delim = 0; delim = *(*optr)++; if (!delim) { errorp1(ERR_BADOPER); } else { while (**optr && **optr != delim) { emit(**optr); (*optr)++; cn += 1; } } cl -> len = cn; } void pseudo_fcs(asmstate_t *as, sourceline_t *cl, char **optr) { int cn = 0; int delim = 0; delim = *(*optr)++; if (!delim) { errorp1(ERR_BADOPER); } else { while (**optr && **optr != delim) { if (!*((*optr) + 1) || *((*optr) + 1) == delim) emit((**optr) | 0x80); else emit(**optr); (*optr)++; cn += 1; } } cl -> len = cn; } void pseudo_fcn(asmstate_t *as, sourceline_t *cl, char **optr) { int cn = 0; int delim = 0; delim = *(*optr)++; if (!delim) { errorp1(ERR_BADOPER); } else { while (**optr && **optr != delim) { emit(**optr); (*optr)++; cn += 1; } } emit(0); cl -> len = cn + 1; } void pseudo_fcb(asmstate_t *as, sourceline_t *cl, char **optr) { int rval, v1; fcb_again: rval = eval_expr(as, cl, optr, &v1); if (v1 < -127 || v1 > 0xff) errorp2(ERR_OVERFLOW); emit(v1 & 0xff); if (**optr == ',') { (*optr)++; goto fcb_again; } } void pseudo_fdb(asmstate_t *as, sourceline_t *cl, char **optr) { int rval, v1; fdb_again: rval = eval_expr(as, cl, optr, &v1); emit((v1 >> 8) & 0xff); emit(v1 & 0xff); if (**optr == ',') { (*optr)++; goto fdb_again; } } void pseudo_fqb(asmstate_t *as, sourceline_t *cl, char **optr) { int rval, v1; fqb_again: rval = eval_expr(as, cl, optr, &v1); emit((v1 >> 24) & 0xff); emit((v1 >> 16) & 0xff); emit((v1 >> 8) & 0xff); emit(v1 & 0xff); if (**optr == ',') { (*optr)++; goto fqb_again; } } // don't need to do anything if we are executing one of these void pseudo_endc(asmstate_t *as, sourceline_t *cl, char **optr) { return; } // if "else" executes, we must be going into an "ignore" state void pseudo_else(asmstate_t *as, sourceline_t *cl, char **optr) { as -> skipcond = 1; as -> skipcount = 1; } void pseudo_ifne(asmstate_t *as, sourceline_t *cl, char **optr) { int v1; int rval; // printf("ifne %s\n", *optr); rval = eval_expr(as, cl, optr, &v1); if (rval < 0) { errorp1(ERR_BADCOND); } else { // printf("Condition value: %d\n", v1); if (!v1) { // printf("condition no match\n"); as -> skipcond = 1; as -> skipcount = 1; } } } void pseudo_ifeq(asmstate_t *as, sourceline_t *cl, char **optr) { int v1; int rval; rval = eval_expr(as, cl, optr, &v1); if (rval < 0) { errorp1(ERR_BADCOND); } else { if (v1) { as -> skipcond = 1; as -> skipcount = 1; } } } void pseudo_iflt(asmstate_t *as, sourceline_t *cl, char **optr) { int v1; int rval; rval = eval_expr(as, cl, optr, &v1); if (rval < 0) { errorp1(ERR_BADCOND); } else { if (v1 >= 0) { as -> skipcond = 1; as -> skipcount = 1; } } } void pseudo_ifle(asmstate_t *as, sourceline_t *cl, char **optr) { int v1; int rval; rval = eval_expr(as, cl, optr, &v1); if (rval < 0) { errorp1(ERR_BADCOND); } else { if (v1 > 0) { as -> skipcond = 1; as -> skipcount = 1; } } } void pseudo_ifgt(asmstate_t *as, sourceline_t *cl, char **optr) { int v1; int rval; rval = eval_expr(as, cl, optr, &v1); if (rval < 0) { errorp1(ERR_BADCOND); } else { if (v1 <= 0) { as -> skipcond = 1; as -> skipcount = 1; } } } void pseudo_ifge(asmstate_t *as, sourceline_t *cl, char **optr) { int v1; int rval; rval = eval_expr(as, cl, optr, &v1); if (rval < 0) { errorp1(ERR_BADCOND); } else { if (v1 < 0) { as -> skipcond = 1; as -> skipcount = 1; } } } void pseudo_error(asmstate_t *as, sourceline_t *cl, char **optr) { cl -> user_error = strdup(*optr); errorp1(ERR_USER); } */