Debian Codegraph @

src/asm/input.c

raw symbols SCIP
1 //
2 // ACME - a crossassembler for producing 6502/65c02/65816 code.
3 // Copyright (C) 1998-2006 Marco Baye
4 // Have a look at "acme.c" for further info
5 //
6 // Input stuff
7
8 #include "config.h"
9 #include "alu.h"
10 #include "dynabuf.h"
11 #include "global.h"
12 #include "input.h"
13 #include "platform.h"
14 #include "section.h"
15 #include "tree.h"
16
17
18 // Constants
19 const char FILE_READBINARY[] = "rb";
20 #define CHAR_TAB (9) // Tab character
21 #define CHAR_LF (10) // line feed (in file)
22 // (10) // start of line (in high-level format)
23 #define CHAR_CR (13) // carriage return (in file)
24 // (13) // end of file (in high-level format)
25 #define CHAR_STATEMENT_DELIMITER ':'
26 #define CHAR_COMMENT_SEPARATOR ';'
27 // If the characters above are changed, don't forget to adjust ByteFlags[]!
28
29 // fake input structure (for error msgs before any real input is established)
30 static input_t outermost = {
31 "<none>", // file name
32 0, // line number
33 FALSE, // Faked file access, so no RAM read
34 INPUTSTATE_EOF, // state of input
35 {
36 NULL // RAM read pointer or file handle
37 }
38 };
39
40
41 // Variables
42 input_t* Input_now = &outermost; // current input structure
43
44
45 // End of source file ("!endoffile" or "!eof")
46 static enum eos_t PO_eof(void) {
47 // Well, it doesn't end right here and now, but at end-of-line! :-)
48 Input_ensure_EOS();
49 Input_now->state = INPUTSTATE_EOF;
50 return(AT_EOS_ANYWAY);
51 }
52
53 // predefined stuff
54 static node_t pseudo_opcodes[] = {
55 PREDEFNODE("eof", PO_eof),
56 PREDEFLAST("endoffile", PO_eof),
57 // ^^^^ this marks the last element
58 };
59
60
61 // Functions
62
63 // register pseudo opcodes
64 void Input_init(void) {
65 Tree_add_table(&pseudo_opcode_tree, pseudo_opcodes);
66 }
67
68 // Let current input point to start of file
69 void Input_new_file(char *src) {
70 /*
71 Input_now->original_filename = ""; //filename;
72 Input_now->line_number = 1;
73 Input_now->source_is_ram = FALSE;
74 Input_now->state = INPUTSTATE_NORMAL;
75 Input_now->src.fd = NULL;
76 */
77 Input_now->original_filename = ""; //filename;
78 Input_now->line_number = 1;
79 Input_now->source_is_ram = FALSE;
80 Input_now->state = INPUTSTATE_NORMAL;
81 Input_now->src.fd = NULL;
82 Input_now->string_source = src;
83 Input_now->string_source_length = strlen(src);
84 Input_now->string_source_position = 0;
85 }
86
87 int hacked_getc(input_t *Input) {
88 if(Input->string_source_position >=
89 Input->string_source_length)
90 return EOF;
91 int byte = *(Input->string_source++);
92 Input->string_source_position++;
93 return byte;
94 }
95
96 // Deliver source code from current file (!) in shortened high-level format
97 static char get_processed_from_file(void) {
98 int from_file;
99
100 do {
101 switch(Input_now->state) {
102
103 case INPUTSTATE_NORMAL:
104 // fetch a fresh byte from the current source file
105 //from_file = getc(Input_now->src.fd);
106 from_file = hacked_getc(Input_now);
107 // now process it
108 /*FALLTHROUGH*/
109
110 case INPUTSTATE_AGAIN:
111 // Process the latest byte again. Of course, this only
112 // makes sense if the loop has executed at least once,
113 // otherwise the contents of from_file are undefined.
114 // If the source is changed so there is a possibility
115 // to enter INPUTSTATE_AGAIN mode without first having
116 // defined "from_file", trouble may arise...
117 Input_now->state = INPUTSTATE_NORMAL;
118 // EOF must be checked first because it cannot be used
119 // as an index into Byte_flags[]
120 if(from_file == EOF) {
121 // remember to send an end-of-file
122 Input_now->state = INPUTSTATE_EOF;
123 return(CHAR_EOS);// end of statement
124 }
125 // check whether character is special one
126 // if not, everything's cool and froody, so return it
127 if((BYTEFLAGS(from_file) & BYTEIS_SYNTAX) == 0)
128 return((char) from_file);
129 // check special characters ("0x00 TAB LF CR SPC :;}")
130 switch(from_file) {
131
132 case CHAR_TAB:// TAB character
133 case ' ':
134 // remember to skip all following blanks
135 Input_now->state = INPUTSTATE_SKIPBLANKS;
136 return(' ');
137
138 case CHAR_LF:// LF character
139 // remember to send a start-of-line
140 Input_now->state = INPUTSTATE_LF;
141 return(CHAR_EOS);// end of statement
142
143 case CHAR_CR:// CR character
144 // remember to check CRLF + send start-of-line
145 Input_now->state = INPUTSTATE_CR;
146 return(CHAR_EOS);// end of statement
147
148 case CHAR_EOB:
149 // remember to send an end-of-block
150 Input_now->state = INPUTSTATE_EOB;
151 return(CHAR_EOS);// end of statement
152
153 case CHAR_STATEMENT_DELIMITER:
154 // just deliver an EOS instead
155 return(CHAR_EOS);// end of statement
156
157 case CHAR_COMMENT_SEPARATOR:
158 // remember to skip remainder of line
159 Input_now->state = INPUTSTATE_COMMENT;
160 return(CHAR_EOS);// end of statement
161
162 default:
163 // complain if byte is 0
164 Throw_error("Source file contains illegal character.");
165 return((char) from_file);
166 }
167
168 case INPUTSTATE_SKIPBLANKS:
169 // read until non-blank, then deliver that
170 do {
171 // from_file = getc(Input_now->src.fd);
172 from_file = hacked_getc(Input_now);
173 } while((from_file == CHAR_TAB) || (from_file == ' '));
174 // re-process last byte
175 Input_now->state = INPUTSTATE_AGAIN;
176 break;
177
178 case INPUTSTATE_LF:
179 // return start-of-line, then continue in normal mode
180 Input_now->state = INPUTSTATE_NORMAL;
181 return(CHAR_SOL);// new line
182
183 case INPUTSTATE_CR:
184 // return start-of-line, remember to check for LF
185 Input_now->state = INPUTSTATE_SKIPLF;
186 return(CHAR_SOL);// new line
187
188 case INPUTSTATE_SKIPLF:
189 from_file = hacked_getc(Input_now);
190 // if LF, ignore it and fetch another byte
191 // otherwise, process current byte
192 if(from_file == CHAR_LF)
193 Input_now->state = INPUTSTATE_NORMAL;
194 else
195 Input_now->state = INPUTSTATE_AGAIN;
196 break;
197
198 case INPUTSTATE_COMMENT:
199 // read until end-of-line or end-of-file
200 do
201 from_file = hacked_getc(Input_now);
202 while((from_file != EOF) && (from_file != CHAR_CR) && (from_file != CHAR_LF));
203 // re-process last byte
204 Input_now->state = INPUTSTATE_AGAIN;
205 break;
206
207 case INPUTSTATE_EOB:
208 // deliver EOB
209 Input_now->state = INPUTSTATE_NORMAL;
210 return(CHAR_EOB);// end of block
211
212 case INPUTSTATE_EOF:
213 // deliver EOF
214 Input_now->state = INPUTSTATE_NORMAL;
215 return(CHAR_EOF);// end of file
216
217 default:
218 Bug_found("StrangeInputMode", Input_now->state);
219 }
220 } while(TRUE);
221 }
222
223 // This function delivers the next byte from the currently active byte source
224 // in shortened high-level format. FIXME - use fn ptr?
225 // When inside quotes, use GetQuotedByte() instead!
226 char GetByte(void) {
227 // do {
228 // If byte source is RAM, then no conversions are
229 // necessary, because in RAM the source already has
230 // high-level format
231 // Otherwise, the source is a file. This means we will call
232 // GetFormatted() which will do a shit load of conversions.
233 if(Input_now->source_is_ram)
234 GotByte = *(Input_now->src.ram_ptr++);
235 else
236 GotByte = get_processed_from_file();
237 // // if start-of-line was read, increment line counter and repeat
238 // if(GotByte != CHAR_SOL)
239 // return(GotByte);
240 // Input_now->line_number++;
241 // } while(TRUE);
242 if(GotByte == CHAR_SOL)
243 Input_now->line_number++;
244 return(GotByte);
245 }
246
247 // This function delivers the next byte from the currently active byte source
248 // in un-shortened high-level format.
249 // This function complains if CHAR_EOS (end of statement) is read.
250 char GetQuotedByte(void) {
251 int from_file; // must be an int to catch EOF
252
253 // If byte source is RAM, then no conversion is necessary,
254 // because in RAM the source already has high-level format
255 if(Input_now->source_is_ram)
256 GotByte = *(Input_now->src.ram_ptr++);
257 // Otherwise, the source is a file.
258 else {
259 // fetch a fresh byte from the current source file
260 from_file = hacked_getc(Input_now);
261 switch(from_file) {
262
263 case EOF:
264 // remember to send an end-of-file
265 Input_now->state = INPUTSTATE_EOF;
266 GotByte = CHAR_EOS; // end of statement
267 break;
268
269 case CHAR_LF:// LF character
270 // remember to send a start-of-line
271 Input_now->state = INPUTSTATE_LF;
272 GotByte = CHAR_EOS; // end of statement
273 break;
274
275 case CHAR_CR:// CR character
276 // remember to check for CRLF + send a start-of-line
277 Input_now->state = INPUTSTATE_CR;
278 GotByte = CHAR_EOS; // end of statement
279 break;
280
281 default:
282 GotByte = from_file;
283 }
284
285 }
286 // now check for end of statement
287 if(GotByte == CHAR_EOS)
288 Throw_error("Quotes still open at end of line.");
289 return(GotByte);
290 }
291
292 // Skip remainder of statement, for example on error
293 void Input_skip_remainder(void) {
294 while(GotByte)
295 GetByte();// Read characters until end-of-statement
296 }
297
298 // Ensure that the remainder of the current statement is empty, for example
299 // after mnemonics using implied addressing.
300 void Input_ensure_EOS(void) {// Now GotByte = first char to test
301 SKIPSPACE();
302 if(GotByte) {
303 Throw_error("Garbage data at end of statement.");
304 Input_skip_remainder();
305 }
306 }
307
308 // Skip or store block (starting with next byte, so call directly after
309 // reading opening brace).
310 // If "Store" is TRUE, the block is read into GlobalDynaBuf, then a copy
311 // is made and a pointer to that is returned.
312 // If "Store" is FALSE, NULL is returned.
313 // After calling this function, GotByte holds '}'. Unless EOF was found first,
314 // but then a serious error would have been thrown.
315 char* Input_skip_or_store_block(bool store) {
316 char byte;
317 int depth = 1; // to find matching block end
318
319 // prepare global dynamic buffer
320 DYNABUF_CLEAR(GlobalDynaBuf);
321 do {
322 byte = GetByte();
323 // if wanted, store
324 if(store)
325 DYNABUF_APPEND(GlobalDynaBuf, byte);
326 // now check for some special characters
327 switch(byte) {
328
329 case CHAR_EOF: // End-of-file in block? Sorry, no way.
330 Throw_serious_error(exception_no_right_brace);
331
332 case '"': // Quotes? Okay, read quoted stuff.
333 case '\'':
334 do {
335 GetQuotedByte();
336 // if wanted, store
337 if(store)
338 DYNABUF_APPEND(GlobalDynaBuf, GotByte);
339 } while((GotByte != CHAR_EOS) && (GotByte != byte));
340 break;
341
342 case CHAR_SOB:
343 depth++;
344 break;
345
346 case CHAR_EOB:
347 depth--;
348 break;
349
350 }
351 } while(depth);
352 // in case of skip, return now
353 if(!store)
354 return(NULL);
355 // otherwise, prepare to return copy of block
356 // add EOF, just to make sure block is never read too far
357 DynaBuf_append(GlobalDynaBuf, CHAR_EOS);
358 DynaBuf_append(GlobalDynaBuf, CHAR_EOF);
359 // return pointer to copy
360 return(DynaBuf_get_copy(GlobalDynaBuf));
361 }
362
363 // Read bytes and add to GlobalDynaBuf until the given terminator (or CHAR_EOS)
364 // is found. Act upon single and double quotes by entering (and leaving) quote
365 // mode as needed (So the terminator does not terminate when inside quotes).
366 void Input_until_terminator(char terminator) {
367 char byte = GotByte;
368
369 do {
370 // Terminator? Exit. EndOfStatement? Exit.
371 if((byte == terminator) || (byte == CHAR_EOS))
372 return;
373 // otherwise, append to GlobalDynaBuf and check for quotes
374 DYNABUF_APPEND(GlobalDynaBuf, byte);
375 if((byte == '"') || (byte == '\'')) {
376 do {
377 // Okay, read quoted stuff.
378 GetQuotedByte();// throws error on EOS
379 DYNABUF_APPEND(GlobalDynaBuf, GotByte);
380 } while((GotByte != CHAR_EOS) && (GotByte != byte));
381 // on error, exit now, before calling GetByte()
382 if(GotByte != byte)
383 return;
384 }
385 byte = GetByte();
386 } while(TRUE);
387 }
388
389 // Append to GlobalDynaBuf while characters are legal for keywords.
390 // Throws "missing string" error if none.
391 // Returns number of characters added.
392 int Input_append_keyword_to_global_dynabuf(void) {
393 int length = 0;
394
395 // add characters to buffer until an illegal one comes along
396 while(BYTEFLAGS(GotByte) & CONTS_KEYWORD) {
397 DYNABUF_APPEND(GlobalDynaBuf, GotByte);
398 length++;
399 GetByte();
400 }
401 if(length == 0)
402 Throw_error(exception_missing_string);
403 return(length);
404 }
405
406 // Check whether GotByte is a dot.
407 // If not, store global zone value.
408 // If yes, store current zone value and read next byte.
409 // Then jump to Input_read_keyword(), which returns length of keyword.
410 int Input_read_zone_and_keyword(zone_t *zone) {
411 SKIPSPACE();
412 if(GotByte == '.') {
413 GetByte();
414 *zone = Section_now->zone;
415 } else
416 *zone = ZONE_GLOBAL;
417 return(Input_read_keyword());
418 }
419
420 // Clear dynamic buffer, then append to it until an illegal (for a keyword)
421 // character is read. Zero-terminate the string. Return its length (without
422 // terminator).
423 // Zero lengths will produce a "missing string" error.
424 int Input_read_keyword(void) {
425 int length;
426
427 DYNABUF_CLEAR(GlobalDynaBuf);
428 length = Input_append_keyword_to_global_dynabuf();
429 // add terminator to buffer (increments buffer's length counter)
430 DynaBuf_append(GlobalDynaBuf, '\0');
431 return(length);
432 }
433
434 // Clear dynamic buffer, then append to it until an illegal (for a keyword)
435 // character is read. Zero-terminate the string, then convert to lower case.
436 // Return its length (without terminator).
437 // Zero lengths will produce a "missing string" error.
438 int Input_read_and_lower_keyword(void) {
439 int length;
440
441 DYNABUF_CLEAR(GlobalDynaBuf);
442 length = Input_append_keyword_to_global_dynabuf();
443 // add terminator to buffer (increments buffer's length counter)
444 DynaBuf_append(GlobalDynaBuf, '\0');
445 DynaBuf_to_lower(GlobalDynaBuf, GlobalDynaBuf);// convert to lower case
446 return(length);
447 }
448
449 // Try to read a file name. If "allow_library" is TRUE, library access by using
450 // <...> quoting is possible as well. The file name given in the assembler
451 // source code is converted from UNIX style to platform style.
452 // Returns whether error occurred (TRUE on error). Filename in GlobalDynaBuf.
453 // Errors are handled and reported, but caller should call
454 // Input_skip_remainder() then.
455 bool Input_read_filename(bool allow_library) {
456 char *lib_prefix,
457 end_quote;
458
459 DYNABUF_CLEAR(GlobalDynaBuf);
460 SKIPSPACE();
461 // check for library access
462 if(GotByte == '<') {
463 // if library access forbidden, complain
464 if(allow_library == FALSE) {
465 Throw_error("Writing to library not supported.");
466 return(TRUE);
467 }
468 // read platform's lib prefix
469 lib_prefix = PLATFORM_LIBPREFIX;
470 #ifndef NO_NEED_FOR_ENV_VAR
471 // if lib prefix not set, complain
472 if(lib_prefix == NULL) {
473 Throw_error("\"ACME\" environment variable not found.");
474 return(TRUE);
475 }
476 #endif
477 // copy lib path and set quoting char
478 DynaBuf_add_string(GlobalDynaBuf, lib_prefix);
479 end_quote = '>';
480 } else {
481 if(GotByte == '"')
482 end_quote = '"';
483 else {
484 Throw_error("File name quotes not found (\"\" or <>).");
485 return(TRUE);
486 }
487 }
488 // read first character, complain if closing quote
489 if(GetQuotedByte() == end_quote) {
490 Throw_error("No file name given.");
491 return(TRUE);
492 }
493 // read characters until closing quote (or EOS) is reached
494 // append platform-converted characters to current string
495 while((GotByte != CHAR_EOS) && (GotByte != end_quote)) {
496 DYNABUF_APPEND(GlobalDynaBuf, PLATFORM_CONVERTPATHCHAR(GotByte));
497 GetQuotedByte();
498 }
499 // on error, return
500 if(GotByte == CHAR_EOS)
501 return(TRUE);
502 GetByte(); // fetch next to forget closing quote
503 // terminate string
504 DynaBuf_append(GlobalDynaBuf, '\0'); // add terminator
505 return(FALSE); // no error
506 }
507
508 // Try to read a comma, skipping spaces before and after. Return TRUE if comma
509 // found, otherwise FALSE.
510 bool Input_accept_comma(void) {
511 SKIPSPACE();
512 if(GotByte != ',')
513 return(FALSE);
514 NEXTANDSKIPSPACE();
515 return(TRUE);
516 }
517
518 // read optional info about parameter length
519 int Input_get_force_bit(void) {
520 char byte;
521 int force_bit = 0;
522
523 if(GotByte == '+') {
524 byte = GetByte();
525 if(byte == '1')
526 force_bit = MVALUE_FORCE08;
527 else if(byte == '2')
528 force_bit = MVALUE_FORCE16;
529 else if(byte == '3')
530 force_bit = MVALUE_FORCE24;
531 if(force_bit)
532 GetByte();
533 else
534 Throw_error("Illegal postfix.");
535 }
536 SKIPSPACE();
537 return(force_bit);
538 }