annotate src/parse.c @ 44:2330b88f9600

Added simple output listing
author lost
date Sun, 04 Jan 2009 06:52:18 +0000
parents 9bd584bb6296
children b9856da2674a
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
36
99e3b3310bac Added missing parse.c file to repository
lost
parents:
diff changeset
1 /*
99e3b3310bac Added missing parse.c file to repository
lost
parents:
diff changeset
2 parse.c
99e3b3310bac Added missing parse.c file to repository
lost
parents:
diff changeset
3 Copyright © 2008 William Astle
99e3b3310bac Added missing parse.c file to repository
lost
parents:
diff changeset
4
99e3b3310bac Added missing parse.c file to repository
lost
parents:
diff changeset
5 This file is part of LWASM.
99e3b3310bac Added missing parse.c file to repository
lost
parents:
diff changeset
6
99e3b3310bac Added missing parse.c file to repository
lost
parents:
diff changeset
7 LWASM is free software: you can redistribute it and/or modify it under the
99e3b3310bac Added missing parse.c file to repository
lost
parents:
diff changeset
8 terms of the GNU General Public License as published by the Free Software
99e3b3310bac Added missing parse.c file to repository
lost
parents:
diff changeset
9 Foundation, either version 3 of the License, or (at your option) any later
99e3b3310bac Added missing parse.c file to repository
lost
parents:
diff changeset
10 version.
99e3b3310bac Added missing parse.c file to repository
lost
parents:
diff changeset
11
99e3b3310bac Added missing parse.c file to repository
lost
parents:
diff changeset
12 This program is distributed in the hope that it will be useful, but WITHOUT
99e3b3310bac Added missing parse.c file to repository
lost
parents:
diff changeset
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
99e3b3310bac Added missing parse.c file to repository
lost
parents:
diff changeset
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
99e3b3310bac Added missing parse.c file to repository
lost
parents:
diff changeset
15 more details.
99e3b3310bac Added missing parse.c file to repository
lost
parents:
diff changeset
16
99e3b3310bac Added missing parse.c file to repository
lost
parents:
diff changeset
17 You should have received a copy of the GNU General Public License along with
99e3b3310bac Added missing parse.c file to repository
lost
parents:
diff changeset
18 this program. If not, see <http://www.gnu.org/licenses/>.
99e3b3310bac Added missing parse.c file to repository
lost
parents:
diff changeset
19 */
99e3b3310bac Added missing parse.c file to repository
lost
parents:
diff changeset
20
99e3b3310bac Added missing parse.c file to repository
lost
parents:
diff changeset
21 /*
99e3b3310bac Added missing parse.c file to repository
lost
parents:
diff changeset
22 Contains the general parser
99e3b3310bac Added missing parse.c file to repository
lost
parents:
diff changeset
23 */
99e3b3310bac Added missing parse.c file to repository
lost
parents:
diff changeset
24
99e3b3310bac Added missing parse.c file to repository
lost
parents:
diff changeset
25 #define __parse_c_seen__
99e3b3310bac Added missing parse.c file to repository
lost
parents:
diff changeset
26
99e3b3310bac Added missing parse.c file to repository
lost
parents:
diff changeset
27 #include <ctype.h>
99e3b3310bac Added missing parse.c file to repository
lost
parents:
diff changeset
28 #include <string.h>
99e3b3310bac Added missing parse.c file to repository
lost
parents:
diff changeset
29
99e3b3310bac Added missing parse.c file to repository
lost
parents:
diff changeset
30 #include "lwasm.h"
99e3b3310bac Added missing parse.c file to repository
lost
parents:
diff changeset
31 #include "instab.h"
99e3b3310bac Added missing parse.c file to repository
lost
parents:
diff changeset
32 #include "util.h"
99e3b3310bac Added missing parse.c file to repository
lost
parents:
diff changeset
33
99e3b3310bac Added missing parse.c file to repository
lost
parents:
diff changeset
34 // parse a line and despatch to the appropriate handlers for opcodes
99e3b3310bac Added missing parse.c file to repository
lost
parents:
diff changeset
35 int lwasm_parse_line(asmstate_t *as, lwasm_line_t *l)
99e3b3310bac Added missing parse.c file to repository
lost
parents:
diff changeset
36 {
99e3b3310bac Added missing parse.c file to repository
lost
parents:
diff changeset
37 char *p, *p2;
99e3b3310bac Added missing parse.c file to repository
lost
parents:
diff changeset
38 char *opc;
99e3b3310bac Added missing parse.c file to repository
lost
parents:
diff changeset
39 int opnum;
44
2330b88f9600 Added simple output listing
lost
parents: 38
diff changeset
40 char *sym = NULL;
36
99e3b3310bac Added missing parse.c file to repository
lost
parents:
diff changeset
41
99e3b3310bac Added missing parse.c file to repository
lost
parents:
diff changeset
42 p = l -> text;
99e3b3310bac Added missing parse.c file to repository
lost
parents:
diff changeset
43
99e3b3310bac Added missing parse.c file to repository
lost
parents:
diff changeset
44 if (!*p)
37
538e15927776 Added symbol handling to expression subsystem; adpated instruction handlers to the new scheme; misc fixes
lost
parents: 36
diff changeset
45 {
538e15927776 Added symbol handling to expression subsystem; adpated instruction handlers to the new scheme; misc fixes
lost
parents: 36
diff changeset
46 as -> context += 1;
36
99e3b3310bac Added missing parse.c file to repository
lost
parents:
diff changeset
47 return 0;
37
538e15927776 Added symbol handling to expression subsystem; adpated instruction handlers to the new scheme; misc fixes
lost
parents: 36
diff changeset
48 }
36
99e3b3310bac Added missing parse.c file to repository
lost
parents:
diff changeset
49
44
2330b88f9600 Added simple output listing
lost
parents: 38
diff changeset
50 // for output generation later
2330b88f9600 Added simple output listing
lost
parents: 38
diff changeset
51 l -> codeaddr = as -> addr;
2330b88f9600 Added simple output listing
lost
parents: 38
diff changeset
52
36
99e3b3310bac Added missing parse.c file to repository
lost
parents:
diff changeset
53 if (!isspace(*p) && *p != '*' && *p != ';')
99e3b3310bac Added missing parse.c file to repository
lost
parents:
diff changeset
54 {
99e3b3310bac Added missing parse.c file to repository
lost
parents:
diff changeset
55 // we have a symbol specified here
99e3b3310bac Added missing parse.c file to repository
lost
parents:
diff changeset
56 // parse it and define
99e3b3310bac Added missing parse.c file to repository
lost
parents:
diff changeset
57 // need to handle local symbols here...
37
538e15927776 Added symbol handling to expression subsystem; adpated instruction handlers to the new scheme; misc fixes
lost
parents: 36
diff changeset
58 for (p2 = p; *p2 && !isspace(*p2); p2++)
538e15927776 Added symbol handling to expression subsystem; adpated instruction handlers to the new scheme; misc fixes
lost
parents: 36
diff changeset
59 /* do nothing */ ;
538e15927776 Added symbol handling to expression subsystem; adpated instruction handlers to the new scheme; misc fixes
lost
parents: 36
diff changeset
60
538e15927776 Added symbol handling to expression subsystem; adpated instruction handlers to the new scheme; misc fixes
lost
parents: 36
diff changeset
61 sym = lwasm_alloc((p2 - p) + 1);
538e15927776 Added symbol handling to expression subsystem; adpated instruction handlers to the new scheme; misc fixes
lost
parents: 36
diff changeset
62 sym[p2 - p] = '\0';
538e15927776 Added symbol handling to expression subsystem; adpated instruction handlers to the new scheme; misc fixes
lost
parents: 36
diff changeset
63 memcpy(sym, p, p2 - p);
538e15927776 Added symbol handling to expression subsystem; adpated instruction handlers to the new scheme; misc fixes
lost
parents: 36
diff changeset
64
44
2330b88f9600 Added simple output listing
lost
parents: 38
diff changeset
65 p = p2;
2330b88f9600 Added simple output listing
lost
parents: 38
diff changeset
66
2330b88f9600 Added simple output listing
lost
parents: 38
diff changeset
67 if (as -> passnum == 1)
2330b88f9600 Added simple output listing
lost
parents: 38
diff changeset
68 {
2330b88f9600 Added simple output listing
lost
parents: 38
diff changeset
69 l -> sym = sym;
2330b88f9600 Added simple output listing
lost
parents: 38
diff changeset
70 // have a symbol; now determine if it is valid and register it
2330b88f9600 Added simple output listing
lost
parents: 38
diff changeset
71 // at the current address of the line
2330b88f9600 Added simple output listing
lost
parents: 38
diff changeset
72 debug_message(1, "Registering symbol '%s' at %04X", sym, as -> addr);
2330b88f9600 Added simple output listing
lost
parents: 38
diff changeset
73 if (lwasm_register_symbol(as, l, sym, as -> addr) < 0)
2330b88f9600 Added simple output listing
lost
parents: 38
diff changeset
74 l -> sym = NULL;
2330b88f9600 Added simple output listing
lost
parents: 38
diff changeset
75 }
37
538e15927776 Added symbol handling to expression subsystem; adpated instruction handlers to the new scheme; misc fixes
lost
parents: 36
diff changeset
76 }
538e15927776 Added symbol handling to expression subsystem; adpated instruction handlers to the new scheme; misc fixes
lost
parents: 36
diff changeset
77 else
538e15927776 Added symbol handling to expression subsystem; adpated instruction handlers to the new scheme; misc fixes
lost
parents: 36
diff changeset
78 {
538e15927776 Added symbol handling to expression subsystem; adpated instruction handlers to the new scheme; misc fixes
lost
parents: 36
diff changeset
79 while (*p && isspace(*p))
38
9bd584bb6296 Added debugging message infrastructure
lost
parents: 37
diff changeset
80 p++;
37
538e15927776 Added symbol handling to expression subsystem; adpated instruction handlers to the new scheme; misc fixes
lost
parents: 36
diff changeset
81 if (!*p)
538e15927776 Added symbol handling to expression subsystem; adpated instruction handlers to the new scheme; misc fixes
lost
parents: 36
diff changeset
82 {
538e15927776 Added symbol handling to expression subsystem; adpated instruction handlers to the new scheme; misc fixes
lost
parents: 36
diff changeset
83 as -> context += 1;
538e15927776 Added symbol handling to expression subsystem; adpated instruction handlers to the new scheme; misc fixes
lost
parents: 36
diff changeset
84 return 0;
538e15927776 Added symbol handling to expression subsystem; adpated instruction handlers to the new scheme; misc fixes
lost
parents: 36
diff changeset
85 }
36
99e3b3310bac Added missing parse.c file to repository
lost
parents:
diff changeset
86 }
99e3b3310bac Added missing parse.c file to repository
lost
parents:
diff changeset
87
99e3b3310bac Added missing parse.c file to repository
lost
parents:
diff changeset
88 // skip white space
99e3b3310bac Added missing parse.c file to repository
lost
parents:
diff changeset
89 while (*p && isspace(*p))
99e3b3310bac Added missing parse.c file to repository
lost
parents:
diff changeset
90 p++;
99e3b3310bac Added missing parse.c file to repository
lost
parents:
diff changeset
91
99e3b3310bac Added missing parse.c file to repository
lost
parents:
diff changeset
92 // if comment or end of line, return
99e3b3310bac Added missing parse.c file to repository
lost
parents:
diff changeset
93 if (!*p || *p == '*' || *p == ';')
37
538e15927776 Added symbol handling to expression subsystem; adpated instruction handlers to the new scheme; misc fixes
lost
parents: 36
diff changeset
94 {
44
2330b88f9600 Added simple output listing
lost
parents: 38
diff changeset
95 if (sym)
2330b88f9600 Added simple output listing
lost
parents: 38
diff changeset
96 lwasm_free(l -> sym);
36
99e3b3310bac Added missing parse.c file to repository
lost
parents:
diff changeset
97 return 0;
37
538e15927776 Added symbol handling to expression subsystem; adpated instruction handlers to the new scheme; misc fixes
lost
parents: 36
diff changeset
98 }
36
99e3b3310bac Added missing parse.c file to repository
lost
parents:
diff changeset
99
99e3b3310bac Added missing parse.c file to repository
lost
parents:
diff changeset
100 // parse the opcode
99e3b3310bac Added missing parse.c file to repository
lost
parents:
diff changeset
101 for (p2 = p; *p2 && !isspace(*p2); p2++)
99e3b3310bac Added missing parse.c file to repository
lost
parents:
diff changeset
102 /* do nothing */ ;
99e3b3310bac Added missing parse.c file to repository
lost
parents:
diff changeset
103
99e3b3310bac Added missing parse.c file to repository
lost
parents:
diff changeset
104 opc = lwasm_alloc((p2 - p) + 1);
99e3b3310bac Added missing parse.c file to repository
lost
parents:
diff changeset
105 memcpy(opc, p, p2 - p);
99e3b3310bac Added missing parse.c file to repository
lost
parents:
diff changeset
106 opc[p2 - p] = '\0';
99e3b3310bac Added missing parse.c file to repository
lost
parents:
diff changeset
107
38
9bd584bb6296 Added debugging message infrastructure
lost
parents: 37
diff changeset
108 debug_message(2, "Found operation code: '%s'", opc);
9bd584bb6296 Added debugging message infrastructure
lost
parents: 37
diff changeset
109
36
99e3b3310bac Added missing parse.c file to repository
lost
parents:
diff changeset
110 // skip intervening whitespace if present
99e3b3310bac Added missing parse.c file to repository
lost
parents:
diff changeset
111 while (*p2 && isspace(*p2))
99e3b3310bac Added missing parse.c file to repository
lost
parents:
diff changeset
112 p2++;
99e3b3310bac Added missing parse.c file to repository
lost
parents:
diff changeset
113
99e3b3310bac Added missing parse.c file to repository
lost
parents:
diff changeset
114 // check for macro (pass 1)
99e3b3310bac Added missing parse.c file to repository
lost
parents:
diff changeset
115
99e3b3310bac Added missing parse.c file to repository
lost
parents:
diff changeset
116 // look up instruction in insn table
99e3b3310bac Added missing parse.c file to repository
lost
parents:
diff changeset
117 for (opnum = 0; instab[opnum].opcode; opnum++)
99e3b3310bac Added missing parse.c file to repository
lost
parents:
diff changeset
118 {
99e3b3310bac Added missing parse.c file to repository
lost
parents:
diff changeset
119 if (!strcasecmp(instab[opnum].opcode, opc))
99e3b3310bac Added missing parse.c file to repository
lost
parents:
diff changeset
120 break;
99e3b3310bac Added missing parse.c file to repository
lost
parents:
diff changeset
121 }
99e3b3310bac Added missing parse.c file to repository
lost
parents:
diff changeset
122
99e3b3310bac Added missing parse.c file to repository
lost
parents:
diff changeset
123 if (!(instab[opnum].opcode) || !(instab[opnum].fn))
99e3b3310bac Added missing parse.c file to repository
lost
parents:
diff changeset
124 {
99e3b3310bac Added missing parse.c file to repository
lost
parents:
diff changeset
125 // invalid operation code, throw error
99e3b3310bac Added missing parse.c file to repository
lost
parents:
diff changeset
126 register_error(as, l, 1, "Invalid operation code '%s'", opc);
44
2330b88f9600 Added simple output listing
lost
parents: 38
diff changeset
127 if (sym)
2330b88f9600 Added simple output listing
lost
parents: 38
diff changeset
128 lwasm_free(l -> sym);
36
99e3b3310bac Added missing parse.c file to repository
lost
parents:
diff changeset
129 lwasm_free(opc);
99e3b3310bac Added missing parse.c file to repository
lost
parents:
diff changeset
130 return -1;
99e3b3310bac Added missing parse.c file to repository
lost
parents:
diff changeset
131 }
99e3b3310bac Added missing parse.c file to repository
lost
parents:
diff changeset
132
99e3b3310bac Added missing parse.c file to repository
lost
parents:
diff changeset
133 // dispatch handler
99e3b3310bac Added missing parse.c file to repository
lost
parents:
diff changeset
134 (instab[opnum].fn)(as, l, &p2, opnum);
99e3b3310bac Added missing parse.c file to repository
lost
parents:
diff changeset
135
99e3b3310bac Added missing parse.c file to repository
lost
parents:
diff changeset
136 lwasm_free(opc);
44
2330b88f9600 Added simple output listing
lost
parents: 38
diff changeset
137 if (sym)
2330b88f9600 Added simple output listing
lost
parents: 38
diff changeset
138 lwasm_free(sym);
36
99e3b3310bac Added missing parse.c file to repository
lost
parents:
diff changeset
139 }