comparison src/list.c @ 35:39d750ee8d34

Added error display and fixed infinite loop in lwasm_parse_line()
author lost
date Fri, 02 Jan 2009 06:07:10 +0000
parents 34568fab6058
children 2330b88f9600
comparison
equal deleted inserted replaced
34:b29eec6f3819 35:39d750ee8d34
1 /* 1 /*
2 list.c 2 list.c
3 Copyright © 2008 William Astle 3 Copyright © 2009 William Astle
4 4
5 This file is part of LWASM. 5 This file is part of LWASM.
6 6
7 LWASM is free software: you can redistribute it and/or modify it under the 7 LWASM is free software: you can redistribute it and/or modify it under the
8 terms of the GNU General Public License as published by the Free Software 8 terms of the GNU General Public License as published by the Free Software
15 more details. 15 more details.
16 16
17 You should have received a copy of the GNU General Public License along with 17 You should have received a copy of the GNU General Public License along with
18 this program. If not, see <http://www.gnu.org/licenses/>. 18 this program. If not, see <http://www.gnu.org/licenses/>.
19 19
20 Contains code for displaying a program listing in lwasm 20 Contains code for displaying a program listings, etc.
21 */ 21 */
22 22
23 //#include <ctype.h> 23 #define __list_c_seen__
24 #include <errno.h> 24
25 #include <stdio.h> 25 #include <stdio.h>
26 //#include <stdlib.h> 26 #include <stdlib.h>
27 #include <string.h> 27
28 #define __list_c_seen__
29 //#include "instab.h"
30 #include "lwasm.h" 28 #include "lwasm.h"
31 29
32 const char *errlist[] = 30 void lwasm_show_errors(asmstate_t *as)
33 { 31 {
34 "No error", 32 lwasm_line_t *l;
35 "Bad opcode", 33 lwasm_error_t *e;
36 "Illegal Symbol",
37 "Multiply defined symbol",
38 "Symbol required but not present",
39 "Forward references not permitted",
40 "Byte overflow",
41 "Phase error",
42 "Bad operand",
43 "Symbol not permitted here",
44 "Undefined symbol",
45 "Bit number out of range",
46 "Invalid expression",
47 "Invalid register",
48 "Bad file name",
49 "ENDM without MACRO",
50 "Redefined macro",
51 "Nested namespace",
52 "Bad condition",
53 "User error",
54 "Bad pragma",
55 ""
56 };
57
58 void list_code(asmstate_t *as)
59 {
60 FILE *lf;
61 sourceline_t *cl;
62 int bn;
63 int c;
64 char *t;
65 34
66 if (as -> listfile && strcmp(as -> listfile, "-")) 35 for (l = as -> lineshead; l; l = l -> next)
67 { 36 {
68 lf = fopen(as -> listfile, "w"); 37 if (l -> err)
69 if (!lf)
70 { 38 {
71 perror("Cannot open list file"); 39 for (e = l -> err; e; e = e -> next)
72 return; 40 {
41 fprintf(stderr, "ERROR: %s\n", e -> mess);
42 }
43 fprintf(stderr, "%s\n", l -> text);
73 } 44 }
74 } 45 }
75 else
76 {
77 lf = stdout;
78 }
79
80 for (cl = as -> source_head; cl; cl = cl -> next)
81 {
82 bn = 0;
83 if (cl -> errors)
84 {
85 errortab_t *e;
86 for (e = cl -> errors; e; e = e -> next)
87 {
88 if (e -> errnum < ERR_MAXERR && e -> errnum != ERR_USER)
89 fprintf(lf, "*****ERROR: %s\n", errlist[e -> errnum]);
90 }
91 if (cl -> user_error)
92 {
93 fprintf(lf, "*****ERROR: %s\n", cl -> user_error);
94 }
95 }
96 if (cl -> skipped)
97 {
98 fprintf(lf, "%-15.15s", "<skipped>");
99 }
100 else if (cl -> macrodef)
101 {
102 fprintf(lf, "%-15.15s", "<macrodef>");
103 }
104 else if (cl -> opcode >= 0 && cl -> numcodebytes > 0)
105 {
106 fprintf(lf, "%04X ", cl -> addr);
107 while (bn < 5 && bn < cl -> numcodebytes)
108 {
109 fprintf(lf, "%02X", cl -> codebytes[bn]);
110 bn++;
111 }
112 while (bn < 5)
113 {
114 fprintf(lf, " ");
115 bn++;
116 }
117 }
118 else if (cl -> addrset || (cl -> len && cl -> numcodebytes == 0))
119 {
120 fprintf(lf, "%04X %10s", cl -> addr, "");
121 }
122 else if (cl -> isequ)
123 {
124 fprintf(lf, " %04X ", cl -> symaddr);
125 }
126 else if (cl -> issetdp)
127 {
128 fprintf(lf, " %02X ", cl -> dpval);
129 }
130 else
131 fprintf(lf, " ");
132 fprintf(lf, " %15.15s:%06d ", cl -> sourcefile, cl -> lineno);
133 // actually display the line from the file
134 for (c = 0, t = cl -> line; *t; t++)
135 {
136 if (*t == '\n' || *t == '\r')
137 break;
138 if (*t == '\t')
139 {
140 do
141 {
142 fprintf(lf, " ");
143 c++;
144 } while (c % 8);
145 }
146 else
147 {
148 c++;
149 fprintf(lf, "%c", *t);
150 }
151 }
152 // fprintf(lf, "\n");
153
154 while (bn < cl -> numcodebytes)
155 {
156 if (bn % 5 == 0)
157 fprintf(lf, "\n%04X ", (cl -> addr + bn) & 0xFFFF);
158 fprintf(lf, "%02X", cl -> codebytes[bn]);
159 bn++;
160 }
161 fprintf(lf, "\n");
162 }
163
164 fprintf(lf, "\n");
165 list_symbols(as, lf);
166
167 if (lf != stdout)
168 fclose(lf);
169 } 46 }
170 47
48 void lwasm_list(asmstate_t *as)
49 {
50 lwasm_show_errors(as);
51 }