Mercurial > hg-old > index.cgi
view lwasm/parse.c @ 264:61d1db1dfe2a 2.6
Build manual for release
author | lost |
---|---|
date | Tue, 22 Dec 2009 05:27:32 +0000 |
parents | 6363b9ebf825 |
children | 6d09310438a4 |
line wrap: on
line source
/* parse.c Copyright © 2008 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/>. */ /* Contains the general parser */ #define __parse_c_seen__ #include <config.h> #include <ctype.h> #include <string.h> #include "lwasm.h" #include "instab.h" #include "util.h" // parse a line and despatch to the appropriate handlers for opcodes int lwasm_parse_line(asmstate_t *as, lwasm_line_t *l) { char *p, *p2; char *opc; int opnum; char *sym = NULL; // if this was a bad op first pass (or otherwise a no-process line) // ignore it if (l -> badop) return; p = l -> text; l -> sect = as -> csect; l -> inmod = as -> inmod; // blank lines are a no brainer if (!*p) { as -> context = lwasm_next_context(as); return 0; } // for output generation later but only on pass 1 // also used by some pseudo ops on pass 2 if (as -> passnum == 1) l -> codeaddr = as -> addr; // check if there is a line number and ignore it if there is if (isdigit(*p)) { for (p2 = p; *p2 && isdigit(*p2); p2++) /* do nothing */ ; // we have a line number, skip it and the *first* // whitespace character if (!*p2 || isspace(*p2)) p = p2 + 1; } // if it's a comment, return (this doesn't cause a context change) if (*p == '*' || *p == ';') return; // if we have C pre-processor directives/output, ignore it if (*p == '#') return; // if we start with a non-space character, it's a symbol symbolagain: if (!isspace(*p)) { // if it starts with a comment character, it's not a symbol if (*p == ';' || *p == '*') return; // we have a symbol specified here // parse it out and record it for later use for (p2 = p; *p2 && !isspace(*p2) && *p2 != ':'; p2++) /* do nothing */ ; sym = lwasm_alloc((p2 - p) + 1); sym[p2 - p] = '\0'; memcpy(sym, p, p2 - p); p = p2; if (!*sym) { register_error(as, l, 1, "Invalid symbol"); lwasm_free(sym); sym = NULL; } if (*p == ':') p++; } l -> sym = sym; // now skip any whitespace to find the opcode while (*p && isspace(*p)) p++; // is the line blank? if (!*p && !sym) { // nothing but white space *is* a context break as -> context = lwasm_next_context(as); return; } // parse the opcode for (p2 = p; *p2 && !isspace(*p2); p2++) /* do nothing */ ; opc = lwasm_alloc((p2 - p) + 1); memcpy(opc, p, p2 - p); opc[p2 - p] = '\0'; if (strchr(opc, ':')) { lwasm_free(opc); goto symbolagain; } l -> forceglobal = 0; // if the opcode contains an =, treat it as "symbol = expr" if (!sym && strchr(opc, '=')) { for (p2 = opc; *p2 && *p2 != '='; p2++) /* do nothing */ ; sym = lwasm_alloc((p2 - opc) + 1); memcpy(sym, opc, p2 - opc); sym[p2 - opc] = '\0'; l -> sym = sym; p2 = p + (p2 - opc) + 1; // p2++; opc[0] = '='; opc[1] = '\0'; debug_message(2, "Found opcode = with symbol %s and operand %s", sym, p2); l -> forceglobal = 1; } debug_message(2, "Found operation code: '%s'", opc); // skip intervening whitespace if present while (*p2 && isspace(*p2)) p2++; // look up instruction in insn table for (opnum = 0; instab[opnum].opcode; opnum++) { if (!strcasecmp(instab[opnum].opcode, opc)) break; } // if we found no operation, check if we had a comment // the reason this check is here is to allow for "private" // operation codes like "*pragma" which will be ignored by // other assemblers // also skip empty ops if (!(instab[opnum].opcode)) { if (*opc == '*' || *opc == ';' || !*opc) goto done_line; } // now we have the opcode and the symbol, we can decide if we're // actually going to do anything with this line // we will NOT call the function if any of the following are true: // - we are skipping a condition and the operation code is not a conditional // - we are defining a macro and the operation code is not ENDM // we will call the function in any other circumstance // first condition above if (as -> inmacro && instab[opnum].endm == 0) { add_macro_line(as, l -> text); goto done_line; } // second condition above if (as -> skipcond && instab[opnum].iscond == 0) goto done_line; if (as -> instruct && as -> passnum == 1) { structtab_t *s = NULL; struct struct_sym_e *e, *e2; // look for a structure if not an acceptable opcode if (!instab[opnum].opcode) { for (s = as -> structs; s; s = s -> next) { if (s == as -> cstruct) continue; if (!strcmp(opc, s -> name)) goto isok; } } // check for a "reservation opcode" if (instab[opnum].opcode && instab[opnum].isreserve) { (instab[opnum].fn)(as, l, &p2, opnum); // if we didn't end on a "space" character or EOL, throw error if (p2 && *p2 && !isspace(*p2) && !(l -> err) && as -> passnum == 1) register_error(as, l, 1, "Bad operand: %s (%d)", p2, as -> passnum); if (as -> instruct == 0) goto done_line; goto isok; } // carp if not valid register_error(as, l, 1, "Only other structs and rmb allowed in struct"); l -> badop = 1; goto done_line; isok: for (e2 = as -> cstruct -> fields; e2 && e2 -> next; e2 = e2 -> next) /* do nothing */ ; e = lwasm_alloc(sizeof(struct struct_sym_e)); e -> next = NULL; e -> substruct = s; if (sym) e -> name = lwasm_strdup(sym); else e -> name = NULL; if (s) e -> size = s -> size; else e -> size = l -> nocodelen; if (e2) e2 -> next = e; else as -> cstruct -> fields = e; as -> cstruct -> size += e -> size; } // we've registered the symbol as needed // now we need to check for a macro call IFF we don't collide with // an operation code; otherwise, call the operation function if (instab[opnum].opcode) { if (instab[opnum].fn && !(as -> no6309 && instab[opnum].is6309)) { (instab[opnum].fn)(as, l, &p2, opnum); // if we didn't end on a "space" character or EOL, throw error if (p2 && *p2 && !isspace(*p2) && !(l -> err) && as -> passnum == 1) register_error(as, l, 1, "Bad operand: %s (%d)", p2, as -> passnum); } else { // carp about unimplemented operation if (instab[opnum].is6309) register_error(as, l, 1, "Use of 6309 operation code: %s", opc); else register_error(as, l, 1, "Unimplemented operation code: %s", opc); } } else { if (expand_macro(as, l, &p2, opc) == 0) goto done_line; if (expand_struct(as, l, &p2, opc) == 0) goto done_line; // carp about an unknown operation code and note that fact for // pass 2 in case a macro appears later with the same name! register_error(as, l, 1, "Uknown operation code: %s", opc); l -> badop = 1; } done_line: if (!(as -> skipcond || as -> inmacro)) { // register symbol if the operation didn't if (sym && instab[opnum].setsym == 0 && as -> instruct == 0) { if (as -> passnum == 1) { debug_message(1, "Registering symbol '%s' at %04X", sym, l -> codeaddr); if (lwasm_register_symbol(as, l, sym, l -> codeaddr, SYMBOL_NORM) < 0) l -> sym = NULL; else l -> addrset = 1; } } } l -> sect = as -> csect; l -> context = as -> context; lwasm_free(opc); if (sym) lwasm_free(sym); }