| 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 | // Section stuff |
| 7 | |
| 8 | #include "config.h" |
| 9 | #include "dynabuf.h" |
| 10 | #include "global.h" |
| 11 | #include "input.h" |
| 12 | #include "tree.h" |
| 13 | #include "section.h" |
| 14 | |
| 15 | |
| 16 | // Constants |
| 17 | static const char type_zone[] = "Zone"; |
| 18 | static const char s_subzone[] = "subzone"; |
| 19 | #define s_zone (s_subzone+3) // Yes, I know I'm sick |
| 20 | static char untitled[] = "<untitled>"; |
| 21 | // ...is actually constant, but flagging it "const" results in heap of warnings |
| 22 | |
| 23 | // fake section structure (for error msgs before any real section is in use) |
| 24 | static section_t initial_section = { |
| 25 | 0, // zone value |
| 26 | "during", // "type" => normally "zone Title" or |
| 27 | "init", // "title" => "macro test", now "during init" |
| 28 | FALSE, // no, title was not malloc'd |
| 29 | }; |
| 30 | |
| 31 | |
| 32 | // Variables |
| 33 | section_t* Section_now = &initial_section;// current section |
| 34 | static section_t outer_section; // outermost section struct |
| 35 | static zone_t zone_max; // Highest zone number yet |
| 36 | |
| 37 | // Write given info into given zone structure and activate it |
| 38 | void Section_new_zone(section_t* section, const char* type, char* title, bool allocated) { |
| 39 | section->zone = ++zone_max; |
| 40 | section->type = type; |
| 41 | section->title = title; |
| 42 | section->allocated = allocated; |
| 43 | // activate new section |
| 44 | Section_now = section; |
| 45 | } |
| 46 | |
| 47 | // Tidy up: If necessary, release section title. |
| 48 | // Warning - the state of the component "Allocd" may have |
| 49 | // changed in the meantime, so don't rely on a local variable. |
| 50 | void Section_finalize(section_t* section) { |
| 51 | if(section->allocated) |
| 52 | free(section->title); |
| 53 | } |
| 54 | |
| 55 | // Switch to new zone ("!zone" or "!zn"). Has to be re-entrant. |
| 56 | static enum eos_t PO_zone(void) { |
| 57 | section_t entry_values;// buffer for outer zone |
| 58 | char* new_title; |
| 59 | bool allocated; |
| 60 | |
| 61 | // remember everything about current structure |
| 62 | entry_values = *Section_now; |
| 63 | // set default values in case there is no valid title |
| 64 | new_title = untitled; |
| 65 | allocated = FALSE; |
| 66 | // Check whether a zone title is given. If yes and it can be read, |
| 67 | // get copy, remember pointer and remember to free it later on. |
| 68 | if(BYTEFLAGS(GotByte) & CONTS_KEYWORD) { |
| 69 | // Because we know of one character for sure, |
| 70 | // there's no need to check the return value. |
| 71 | Input_read_keyword(); |
| 72 | new_title = DynaBuf_get_copy(GlobalDynaBuf); |
| 73 | allocated = TRUE; |
| 74 | } |
| 75 | // setup new section |
| 76 | // section type is "subzone", just in case a block follows |
| 77 | Section_new_zone(Section_now, "Subzone", new_title, allocated); |
| 78 | if(Parse_optional_block()) { |
| 79 | // Block has been parsed, so it was a SUBzone. |
| 80 | Section_finalize(Section_now);// end inner zone |
| 81 | *Section_now = entry_values;// restore entry values |
| 82 | } else { |
| 83 | // no block found, so it's a normal zone change |
| 84 | Section_finalize(&entry_values);// end outer zone |
| 85 | Section_now->type = type_zone;// change type to "zone" |
| 86 | } |
| 87 | return(ENSURE_EOS); |
| 88 | } |
| 89 | |
| 90 | // Start subzone ("!subzone" or "!sz"). Has to be re-entrant. |
| 91 | static enum eos_t PO_subzone(void) { |
| 92 | // output deprecation warning |
| 93 | Throw_first_pass_warning("\"!subzone {}\" is deprecated; use \"!zone {}\" instead."); |
| 94 | // call "!zone" instead |
| 95 | return(PO_zone()); |
| 96 | } |
| 97 | |
| 98 | // predefined stuff |
| 99 | static node_t pseudo_opcodes[] = { |
| 100 | PREDEFNODE(s_zone, PO_zone), |
| 101 | PREDEFNODE("zn", PO_zone), |
| 102 | PREDEFNODE(s_subzone, PO_subzone), |
| 103 | PREDEFLAST("sz", PO_subzone), |
| 104 | // ^^^^ this marks the last element |
| 105 | }; |
| 106 | |
| 107 | // register pseudo opcodes |
| 108 | void Section_init(void) { |
| 109 | Tree_add_table(&pseudo_opcode_tree, pseudo_opcodes); |
| 110 | } |
| 111 | |
| 112 | // Setup outermost section |
| 113 | void Section_passinit(void) { |
| 114 | zone_max = ZONE_GLOBAL;// will be incremented by next line |
| 115 | Section_new_zone(&outer_section, type_zone, untitled, FALSE); |
| 116 | } |