| 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 | // Label stuff |
| 7 | |
| 8 | #include <stdio.h> |
| 9 | #include "acme.h" |
| 10 | #include "alu.h" |
| 11 | #include "cpu.h" |
| 12 | #include "dynabuf.h" |
| 13 | #include "global.h" |
| 14 | #include "input.h" |
| 15 | #include "label.h" |
| 16 | #include "platform.h" |
| 17 | #include "section.h" |
| 18 | #include "tree.h" |
| 19 | |
| 20 | |
| 21 | // Constants |
| 22 | #define WARN_IF_LABEL_INDENTED 1 // FIXME - use CLI switch? |
| 23 | #define s_sl (s_asl+1) // Yes, I know I'm sick |
| 24 | |
| 25 | |
| 26 | // Variables |
| 27 | node_ra_t* Label_forest[256];// ... (because of 8-bit hash) |
| 28 | |
| 29 | |
| 30 | // Dump label value and flags to dump file |
| 31 | static void dump_one_label(node_ra_t* node, FILE* fd) { |
| 32 | label_t* label = node->body; |
| 33 | |
| 34 | // output name |
| 35 | fprintf(fd, "%s", node->id_string); |
| 36 | switch(label->result.flags & MVALUE_FORCEBITS) { |
| 37 | |
| 38 | case MVALUE_FORCE16: |
| 39 | fprintf(fd, "+2="); |
| 40 | break; |
| 41 | |
| 42 | case MVALUE_FORCE16 | MVALUE_FORCE24: |
| 43 | /*FALLTHROUGH*/ |
| 44 | |
| 45 | case MVALUE_FORCE24: |
| 46 | fprintf(fd, "+3="); |
| 47 | break; |
| 48 | |
| 49 | default: |
| 50 | fprintf(fd, " ="); |
| 51 | |
| 52 | } |
| 53 | if(label->result.flags & MVALUE_DEFINED) { |
| 54 | if(label->result.flags & MVALUE_IS_FP) |
| 55 | fprintf(fd, "%.30f", label->result.val.fpval);//FIXME %g |
| 56 | else |
| 57 | fprintf(fd, "$%x", (unsigned) label->result.val.intval); |
| 58 | } else |
| 59 | fprintf(fd, " ?"); |
| 60 | if(label->result.flags & MVALUE_UNSURE) |
| 61 | fprintf(fd, "; ?"); |
| 62 | if(label->usage == 0) |
| 63 | fprintf(fd, "; unused"); |
| 64 | fprintf(fd, "\n"); |
| 65 | } |
| 66 | |
| 67 | // Search for label. Create if nonexistant. If created, give it flags "Flags". |
| 68 | // The label name must be held in GlobalDynaBuf. |
| 69 | label_t* Label_find(zone_t zone, int flags) { |
| 70 | node_ra_t* node; |
| 71 | label_t* label; |
| 72 | bool node_created; |
| 73 | int force_bits = flags & MVALUE_FORCEBITS; |
| 74 | |
| 75 | node_created = Tree_hard_scan(&node, Label_forest, zone, TRUE); |
| 76 | // if node has just been created, create label as well |
| 77 | if(node_created) { |
| 78 | // Create new label structure |
| 79 | label = safe_malloc(sizeof(label_t)); |
| 80 | // Finish empty label item |
| 81 | label->result.flags = flags; |
| 82 | if(flags & MVALUE_IS_FP) |
| 83 | label->result.val.fpval = 0; |
| 84 | else |
| 85 | label->result.val.intval = 0; |
| 86 | label->usage = 0;// usage count |
| 87 | label->pass = pass_count; |
| 88 | node->body = label; |
| 89 | } else |
| 90 | label = node->body; |
| 91 | // make sure the force bits don't clash |
| 92 | if((node_created == FALSE) && force_bits) |
| 93 | if((label->result.flags & MVALUE_FORCEBITS) != force_bits) |
| 94 | Throw_error("Too late for postfix."); |
| 95 | return(label); |
| 96 | } |
| 97 | |
| 98 | // Assign value to label. The function acts upon the label's flag bits and |
| 99 | // produces an error if needed. |
| 100 | void Label_set_value(label_t* label, result_t* new, bool change_allowed) { |
| 101 | int oldflags = label->result.flags; |
| 102 | |
| 103 | // value stuff |
| 104 | if((oldflags & MVALUE_DEFINED) && (change_allowed == FALSE)) { |
| 105 | // Label is already defined, so compare new and old values |
| 106 | // if different type OR same type but different value, complain |
| 107 | if(((oldflags ^ new->flags) & MVALUE_IS_FP) |
| 108 | || ((oldflags & MVALUE_IS_FP) |
| 109 | ? (label->result.val.fpval != new->val.fpval) |
| 110 | : (label->result.val.intval != new->val.intval))) |
| 111 | Throw_error("Label already defined."); |
| 112 | } else |
| 113 | // Label is not defined yet OR redefinitions are allowed |
| 114 | label->result = *new; |
| 115 | // flags stuff |
| 116 | // Ensure that "unsure" labels without "isByte" state don't get that |
| 117 | if((oldflags & (MVALUE_UNSURE | MVALUE_ISBYTE)) == MVALUE_UNSURE) |
| 118 | new->flags &= ~MVALUE_ISBYTE; |
| 119 | if(change_allowed) |
| 120 | oldflags = (oldflags & MVALUE_UNSURE) | new->flags; |
| 121 | else { |
| 122 | if((oldflags & MVALUE_FORCEBITS) == 0) |
| 123 | if((oldflags & (MVALUE_UNSURE | MVALUE_DEFINED)) == 0) |
| 124 | oldflags |= new->flags & MVALUE_FORCEBITS; |
| 125 | oldflags |= new->flags & ~MVALUE_FORCEBITS; |
| 126 | } |
| 127 | label->result.flags = oldflags; |
| 128 | } |
| 129 | |
| 130 | // (Re)set label |
| 131 | static enum eos_t PO_set(void) {// Now GotByte = illegal char |
| 132 | result_t result; |
| 133 | int force_bit; |
| 134 | label_t* label; |
| 135 | zone_t zone; |
| 136 | |
| 137 | if(Input_read_zone_and_keyword(&zone) == 0) // skips spaces before |
| 138 | // Now GotByte = illegal char |
| 139 | return(SKIP_REMAINDER); |
| 140 | force_bit = Input_get_force_bit();// skips spaces after |
| 141 | label = Label_find(zone, force_bit); |
| 142 | if(GotByte != '=') { |
| 143 | Throw_error(exception_syntax); |
| 144 | return(SKIP_REMAINDER); |
| 145 | } |
| 146 | // label = parsed value |
| 147 | GetByte();// proceed with next char |
| 148 | ALU_any_result(&result); |
| 149 | // clear label's force bits and set new ones |
| 150 | label->result.flags &= ~(MVALUE_FORCEBITS | MVALUE_ISBYTE); |
| 151 | if(force_bit) { |
| 152 | label->result.flags |= force_bit; |
| 153 | result.flags &= ~(MVALUE_FORCEBITS | MVALUE_ISBYTE); |
| 154 | } |
| 155 | Label_set_value(label, &result, TRUE); |
| 156 | return(ENSURE_EOS); |
| 157 | } |
| 158 | |
| 159 | // Select dump file |
| 160 | static enum eos_t PO_sl(void) { |
| 161 | // only process this pseudo opcode in the first pass |
| 162 | if(pass_count) |
| 163 | return(SKIP_REMAINDER); |
| 164 | // if label dump file already chosen, complain and exit |
| 165 | if(labeldump_filename) { |
| 166 | Throw_warning("Label dump file already chosen."); |
| 167 | return(SKIP_REMAINDER); |
| 168 | } |
| 169 | // read filename to global dynamic buffer |
| 170 | // if no file name given, exit (complaining will have been done) |
| 171 | if(Input_read_filename(FALSE)) |
| 172 | return(SKIP_REMAINDER); |
| 173 | // get malloc'd copy of filename |
| 174 | labeldump_filename = DynaBuf_get_copy(GlobalDynaBuf); |
| 175 | // ensure there's no garbage at end of line |
| 176 | return(ENSURE_EOS); |
| 177 | } |
| 178 | |
| 179 | // predefined stuff |
| 180 | static node_t pseudo_opcodes[] = { |
| 181 | PREDEFNODE("set", PO_set), |
| 182 | PREDEFLAST(s_sl, PO_sl), |
| 183 | // ^^^^ this marks the last element |
| 184 | }; |
| 185 | |
| 186 | // Parse implicit label definition (can be either global or local). |
| 187 | // GlobalDynaBuf holds the label name. |
| 188 | void Label_implicit_definition(zone_t zone, int stat_flags, int force_bit, bool change) { |
| 189 | result_t result; |
| 190 | label_t* label; |
| 191 | |
| 192 | label = Label_find(zone, force_bit); |
| 193 | // implicit label definition (label) |
| 194 | if((stat_flags & SF_FOUND_BLANK) && WARN_IF_LABEL_INDENTED) |
| 195 | Throw_first_pass_warning("Implicit label definition not in leftmost column."); |
| 196 | result.flags = CPU_pc.flags & MVALUE_DEFINED; |
| 197 | result.val.intval = CPU_pc.intval; |
| 198 | Label_set_value(label, &result, change); |
| 199 | } |
| 200 | |
| 201 | // Parse label definition (can be either global or local). |
| 202 | // GlobalDynaBuf holds the label name. |
| 203 | void Label_parse_definition(zone_t zone, int stat_flags) { |
| 204 | result_t result; |
| 205 | label_t* label; |
| 206 | int force_bit = Input_get_force_bit();// skips spaces after |
| 207 | // FIXME - force bit is allowed for implicit label defs?! |
| 208 | |
| 209 | if(GotByte == '=') { |
| 210 | // explicit label definition (label = <something>) |
| 211 | label = Label_find(zone, force_bit); |
| 212 | // label = parsed value |
| 213 | GetByte(); // skip '=' |
| 214 | ALU_any_result(&result); |
| 215 | Label_set_value(label, &result, FALSE); |
| 216 | Input_ensure_EOS(); |
| 217 | } else |
| 218 | Label_implicit_definition(zone, stat_flags, force_bit, FALSE); |
| 219 | } |
| 220 | |
| 221 | // Dump global labels to file |
| 222 | void Label_dump_all(FILE* fd) { |
| 223 | Tree_dump_forest(Label_forest, ZONE_GLOBAL, dump_one_label, fd); |
| 224 | PLATFORM_SETFILETYPE_TEXT(labeldump_filename); |
| 225 | } |
| 226 | |
| 227 | // register pseudo opcodes and clear label forest |
| 228 | void Label_init(void) { |
| 229 | node_ra_t** ptr; |
| 230 | int i; |
| 231 | |
| 232 | Tree_add_table(&pseudo_opcode_tree, pseudo_opcodes); |
| 233 | // Cut down all the trees (clear pointer table) |
| 234 | ptr = Label_forest; |
| 235 | // Clear pointertable |
| 236 | for(i = 255; i >= 0; i--) |
| 237 | *ptr++ = NULL; |
| 238 | } |
| 239 | |
| 240 | // Fix name of anonymous forward label (held in DynaBuf, NOT TERMINATED!) so it |
| 241 | // references the *next* anonymous forward label definition. The tricky bit is, |
| 242 | // each name length would need its own counter. But hey, ACME's real quick in |
| 243 | // finding labels, so I'll just abuse the label system to store those counters. |
| 244 | label_t* Label_fix_forward_name(void) { |
| 245 | label_t* counter_label; |
| 246 | unsigned long number; |
| 247 | |
| 248 | // terminate name, find "counter" label and read value |
| 249 | DynaBuf_append(GlobalDynaBuf, '\0'); |
| 250 | counter_label = Label_find(Section_now->zone, 0); |
| 251 | // make sure it gets reset to zero in each new pass |
| 252 | if(counter_label->pass != pass_count) { |
| 253 | counter_label->pass = pass_count; |
| 254 | counter_label->result.val.intval = 0; |
| 255 | } |
| 256 | number = (unsigned long) counter_label->result.val.intval; |
| 257 | // now append to the name to make it unique |
| 258 | GlobalDynaBuf->size--; // forget terminator, we want to append |
| 259 | do { |
| 260 | DYNABUF_APPEND(GlobalDynaBuf, 'a' + (number & 15)); |
| 261 | number >>= 4; |
| 262 | } while(number); |
| 263 | DynaBuf_append(GlobalDynaBuf, '\0'); |
| 264 | return(counter_label); |
| 265 | } |