| 1 | // ACME - a crossassembler for producing 6502/65c02/65816/65ce02 code. |
| 2 | // Copyright (C) 1998-2025 Marco Baye |
| 3 | // Have a look at "acme.c" for further info |
| 4 | // |
| 5 | // Global stuff - things that are needed by several modules |
| 6 | // 19 Nov 2014 Merged Johann Klasek's report listing generator patch |
| 7 | // 23 Nov 2014 Merged Martin Piper's "--msvc" error output patch |
| 8 | #ifndef global_H |
| 9 | #define global_H |
| 10 | |
| 11 | |
| 12 | #include <stdio.h> // for FILE |
| 13 | #include "config.h" |
| 14 | |
| 15 | |
| 16 | // constants |
| 17 | |
| 18 | #define LOCAL_PREFIX '.' // DO NOT CHANGE (or change expression parser accordingly) |
| 19 | #define CHEAP_PREFIX '@' // prefix character for cheap locals |
| 20 | #define NO_VALUE_GIVEN (-1) // used when only non-negative values are valid, e.g. for addresses |
| 21 | |
| 22 | extern char s_untitled[]; |
| 23 | // error messages during assembly |
| 24 | extern const char exception_negative_size[]; |
| 25 | extern const char exception_no_left_brace[]; |
| 26 | extern const char exception_no_memory_left[]; |
| 27 | extern const char exception_no_right_brace[]; |
| 28 | //extern const char exception_not_yet[]; |
| 29 | extern const char exception_number_out_of_range[]; |
| 30 | extern const char exception_number_out_of_8b_range[]; |
| 31 | extern const char exception_pc_undefined[]; |
| 32 | extern const char exception_symbol_defined[]; |
| 33 | extern const char exception_syntax[]; |
| 34 | // byte flags table |
| 35 | extern const char global_byte_flags[]; |
| 36 | #define BYTE_STARTS_KEYWORD(b) (global_byte_flags[(unsigned char) b] & (1u << 7)) // byte is allowed at start of keyword (a-z, A-Z, _, everything>127) |
| 37 | #define BYTE_CONTINUES_KEYWORD(b) (global_byte_flags[(unsigned char) b] & (1u << 6)) // byte is allowed in a keyword (as above, plus digits) |
| 38 | //#define BYTE_TO_LOWER_CASE(b) bit 5 means: "byte is upper case, and can be converted to lower case by ORing this bit" - but this is not used at the moment! |
| 39 | #define BYTE_IS_SYNTAX_CHAR(b) (global_byte_flags[(unsigned char) b] & (1u << 4)) // special character for input syntax |
| 40 | #define BYTE_FOLLOWS_ANON(b) (global_byte_flags[(unsigned char) b] & (1u << 3)) // preceding '-' are backward label |
| 41 | // bits 2, 1 and 0 are currently unused |
| 42 | |
| 43 | enum dialect { |
| 44 | V0_85__OLDEST_SUPPORTED, // v0.85 looks like the oldest version it makes sense to actually support |
| 45 | V0_86__DEPRECATE_REALPC, // v0.86 made !pseudopc/!realpc give a warning to use !pseudopc{} instead, and !to wants a file format |
| 46 | V0_93__SHORTER_SETPC_WARNING, // v0.93 claimed to allow *= inside !pseudopc blocks, but didn't. It shortened the warning, but '}' or !realpc clobbered PC |
| 47 | V0_94_6__RIGHT_ASSOC_POWER, // v0.94.6 made "power of" operator right-associative |
| 48 | // v0.94.7 fixed a bug: empty code segments no longer included in output file |
| 49 | V0_94_8__DISABLED_OBSOLETE, // v0.94.8 made *= work inside !pseudopc, disabled !cbm/!realpc/!subzone |
| 50 | V0_94_12__NEW_FOR_SYNTAX, // v0.94.12 introduced the new "!for" syntax |
| 51 | V0_95_2__NEW_ANC_OPCODE, // v0.95.2 changed ANC#8 from opcode 0x2b to 0x0b |
| 52 | V0_97__BACKSLASH_ESCAPING, // v0.97 introduced backslash escaping (and therefore strings) |
| 53 | V__CURRENT_VERSION, // "RELEASE" |
| 54 | V0_98__PATHS_AND_SYMBOLCHANGE, // v0.98 fixes paths and allows symbols to change |
| 55 | // possible changes in future versions: |
| 56 | // ignore leading zeroes? |
| 57 | V__FUTURE_VERSION // future (for testing new features) |
| 58 | }; |
| 59 | enum debuglevel { |
| 60 | DEBUGLEVEL_SERIOUS = -3, // ACME stops right away |
| 61 | DEBUGLEVEL_ERROR = -2, // something is wrong |
| 62 | DEBUGLEVEL_WARNING = -1, // something looks wrong |
| 63 | DEBUGLEVEL_INFO = 0, // info msg ("173 bytes left in code area!") |
| 64 | DEBUGLEVEL_DEBUG = 1 // debug msg |
| 65 | // debug messages with higher levels are suppressed, |
| 66 | // can be changed using "--debuglevel" cli switch. |
| 67 | }; |
| 68 | enum outfile_format { |
| 69 | OUTFILE_FORMAT_UNSPECIFIED, // default (uses "plain" actually) |
| 70 | OUTFILE_FORMAT_PLAIN, // no header, just code |
| 71 | OUTFILE_FORMAT_CBM, // 16-bit load address, code (default for "!to" pseudo opcode) |
| 72 | OUTFILE_FORMAT_APPLE // 16-bit load address, 16-bit length, code |
| 73 | }; |
| 74 | // configuration |
| 75 | struct config { |
| 76 | char pseudoop_prefix; // '!' or '.' |
| 77 | int process_verbosity; // level of additional output |
| 78 | boolean warn_on_indented_labels; // warn if indented label is encountered |
| 79 | boolean warn_on_type_mismatch; // use type-checking system |
| 80 | int warn_bin_mask; // bitmask for digit counter of binary literals |
| 81 | int max_errors; // errors before giving up |
| 82 | int sanity_limit; // max recursion depth for "!src" and macro calls, also max number of passes |
| 83 | boolean format_msvc; // enabled by --msvc |
| 84 | boolean format_color; // enabled by --color |
| 85 | FILE *msg_stream; // defaults to stderr, changed to stdout by --use-stdout |
| 86 | boolean honor_leading_zeroes; // TRUE, disabled by --ignore-zeroes |
| 87 | boolean strict_segments; // FALSE, enabled by --strict-segments and V0.98 |
| 88 | boolean all_warnings_are_errors; // FALSE, enabled by --strict |
| 89 | boolean test_new_features; // FALSE, enabled by --test |
| 90 | enum dialect dialect; // set by --dialect (and --test --test) |
| 91 | int politeness; // incremented by --please |
| 92 | int debuglevel; // set by --debuglevel, used by "!debug" |
| 93 | const char *platform_lib_prefix; // default value depends on platform |
| 94 | const struct cpu_type *initial_cpu_type; |
| 95 | const char *symbollist_filename; |
| 96 | const char *vicelabels_filename; |
| 97 | const char *output_filename; // TODO - put in "part" struct |
| 98 | enum outfile_format outfile_format; |
| 99 | const char *report_filename; // TODO - put in "part" struct |
| 100 | // default value for these fields (if cli switch not used) is NO_VALUE_GIVEN: |
| 101 | int mem_init_value; // set by --initmem |
| 102 | intval_t initial_pc; // set by --setpc |
| 103 | intval_t outfile_start; // set by --from-to |
| 104 | intval_t outfile_limit; // end+1, set by --from-to |
| 105 | }; |
| 106 | extern struct config config; |
| 107 | |
| 108 | enum shortcut { |
| 109 | SHORTCUT_NONE, // normal execution |
| 110 | SHORTCUT_BREAK, // after "!break" |
| 111 | SHORTCUT_CONT, // after "!continue" |
| 112 | SHORTCUT_RETURN // after "!return" |
| 113 | }; |
| 114 | struct pass { |
| 115 | int number; // counts up from one |
| 116 | struct { |
| 117 | int undefineds; // counts undefined expression results (if this stops decreasing, next pass must list them as errors) |
| 118 | //int needvalue; // counts undefined expression results actually needed for output (when this hits zero, we're done) |
| 119 | // okay, this "needvalue" idea is problematic: evaluating "dec(UNDEFINED)" gives |
| 120 | // a defined result, namely an empty string. so the evaluation has to increment |
| 121 | // *some* counter to make sure more passes are done. but it cannot increment the |
| 122 | // "needvalue" counter because the expression parser does not know whether the |
| 123 | // result is actually put into memory! |
| 124 | int symbolchanges; // count symbol changes (if nonzero, another pass is needed) |
| 125 | int errors; // if nonzero -> stop after this pass |
| 126 | int warnings; // this is needed for showing macro call stack |
| 127 | int suppressed_errors; // this is for |
| 128 | // "errors not reported because pass flags said so", because |
| 129 | // then we can read the value after all symbol changes have |
| 130 | // finally settled and know if the next pass is a victory lap or |
| 131 | // an error output pass. |
| 132 | } counters; |
| 133 | struct { |
| 134 | boolean complain_about_undefined; // will be FALSE until error pass is needed |
| 135 | boolean throw_all_errors; // will be FALSE until error pass is needed |
| 136 | boolean is_final_pass; // needed for warnings like "converted float to int for xor" |
| 137 | boolean generate_output; // create output and/or report file |
| 138 | } flags; |
| 139 | }; |
| 140 | extern struct pass pass; |
| 141 | |
| 142 | struct sanity { |
| 143 | int macro_recursions_left; // for macro calls |
| 144 | int source_recursions_left; // for "!src" |
| 145 | }; |
| 146 | extern struct sanity sanity; |
| 147 | |
| 148 | // report stuff |
| 149 | #define REPORT_ASCBUFSIZE 1024 |
| 150 | #define REPORT_BINBUFSIZE 9 // eight are shown, then "..." |
| 151 | struct report { |
| 152 | FILE *fd; // report file descriptor (NULL => no report) |
| 153 | boolean new_input; |
| 154 | size_t asc_used; |
| 155 | size_t bin_used; |
| 156 | int bin_address; // address at start of bin_buf[] |
| 157 | char asc_buf[REPORT_ASCBUFSIZE]; // source bytes |
| 158 | char bin_buf[REPORT_BINBUFSIZE]; // output bytes |
| 159 | }; |
| 160 | extern struct report *report; // TODO - put in "part" struct |
| 161 | |
| 162 | // Macros for skipping a single space character |
| 163 | #define SKIPSPACE() \ |
| 164 | do { \ |
| 165 | if (GotByte == ' ') \ |
| 166 | GetByte(); \ |
| 167 | } while (0) |
| 168 | #define NEXTANDSKIPSPACE() \ |
| 169 | do { \ |
| 170 | if (GetByte() == ' ') \ |
| 171 | GetByte(); \ |
| 172 | } while (0) |
| 173 | |
| 174 | |
| 175 | // prototypes |
| 176 | |
| 177 | // set configuration to default values |
| 178 | extern void config_default(struct config *conf); |
| 179 | |
| 180 | // allocate memory and die if not available |
| 181 | extern void *safe_malloc(size_t amount); |
| 182 | |
| 183 | // set new value for "we are in !addr block" flag, |
| 184 | // return old value |
| 185 | // (called by "!addr { }") |
| 186 | extern boolean parser_change_addr_block_flag(boolean new_value); |
| 187 | |
| 188 | // set new value for "we are in !nowarn block" flag, |
| 189 | // return old value |
| 190 | // (called by "!nowarn { }") |
| 191 | extern boolean parser_change_nowarn_block_flag(boolean new_value); |
| 192 | |
| 193 | // called by "!addr" pseudo op if used without block |
| 194 | extern void parser_set_addr_prefix(void); |
| 195 | |
| 196 | // called by "!nowarn" pseudo op if used without block |
| 197 | extern void parser_set_nowarn_prefix(void); |
| 198 | |
| 199 | // call with symbol name in GlobalDynaBuf and '=' already eaten. |
| 200 | // "powers" is for "!set" pseudo opcode so changes are allowed (see symbol.h for powers) |
| 201 | extern void parse_assignment(scope_t scope, bits force_bit, bits powers); |
| 202 | |
| 203 | // Parse block, beginning with next byte. |
| 204 | // End reason (either CHAR_EOB or CHAR_EOF) can be found in GotByte afterwards |
| 205 | // Has to be re-entrant. |
| 206 | extern void parse_until_eob_or_eof(void); |
| 207 | |
| 208 | // Skip space. If GotByte is CHAR_SOB ('{'), parse block and return TRUE. |
| 209 | // Otherwise (if there is no block), return FALSE. |
| 210 | // Don't forget to call EnsureEOL() afterwards. |
| 211 | extern int parse_optional_block(void); |
| 212 | |
| 213 | // parse a whole source code file |
| 214 | // file name must be given in platform style, i.e. |
| 215 | // "directory/basename.extension" on linux, |
| 216 | // "directory.basename/extension" on RISC OS, etc. |
| 217 | // and the pointer must remain valid forever! |
| 218 | extern void parse_source_code_file(FILE *fd, const char *eternal_plat_filename); |
| 219 | |
| 220 | // read optional info about parameter length |
| 221 | extern bits parser_get_force_bit(void); |
| 222 | |
| 223 | // return current shortcut state |
| 224 | extern enum shortcut parser_get_shortcut(void); |
| 225 | |
| 226 | // start or end processing a !break/!continue/!return keyword |
| 227 | extern void parser_set_shortcut(enum shortcut); |
| 228 | |
| 229 | // return current state and set new state of "allow !break and !continue" flag |
| 230 | extern boolean parser_allow_break_cont(boolean new_state); |
| 231 | |
| 232 | // return current state and set new state of "allow !return" flag |
| 233 | extern boolean parser_allow_return(boolean new_state); |
| 234 | |
| 235 | // generate a debug/info/warning/error message |
| 236 | // if the "optional alternative location" given is NULL, the current location is used |
| 237 | extern void throw_message(enum debuglevel level, const char msg[], struct location *opt_alt_loc); |
| 238 | |
| 239 | // output a warning (something looks wrong) |
| 240 | extern void throw_warning(const char msg[]); |
| 241 | |
| 242 | // output an error (something is wrong, no output file will be generated). |
| 243 | // the assembler will try to go on with the assembly, so the user gets to know |
| 244 | // about more than one of their typos at a time. |
| 245 | extern void throw_error(const char msg[]); |
| 246 | |
| 247 | // output a serious error (assembly stops, for example if outbuffer overruns). |
| 248 | extern void throw_serious_error(const char msg[]); |
| 249 | |
| 250 | // output a warning, but only if we are in first pass, otherwise suppress |
| 251 | // (only use this if using throw_finalpass_warning is impossible, |
| 252 | // for example in cases where the code is skipped in later passes) |
| 253 | extern void throw_pass1_warning(const char msg[]); |
| 254 | |
| 255 | // output a warning, but only if we are in final pass, otherwise suppress |
| 256 | // (for example "converted float to int for XOR" -> must be done in |
| 257 | // final pass because values might be undefined earlier!) |
| 258 | extern void throw_finalpass_warning(const char msg[]); |
| 259 | |
| 260 | // throw "macro twice"/"symbol twice" error |
| 261 | // first output error as "error", then location of initial definition as "info" |
| 262 | extern void throw_redef_error(const char error_msg[], struct location *old_def, const char info_msg[]); |
| 263 | |
| 264 | // ugly kluge, switches throw_error() from cli args mode to normal mode |
| 265 | extern void throw__done_with_cli_args(void); |
| 266 | |
| 267 | // handle bugs |
| 268 | extern void BUG(const char *msg, int code); |
| 269 | |
| 270 | // process error that might vanish if symbols change: |
| 271 | // if current pass is an "error output" pass, actually throw error. |
| 272 | // otherwise just increment counter to let mainloop know this pass wasn't successful. |
| 273 | extern void countorthrow_value_error(const char *msg); |
| 274 | |
| 275 | // insert object (in case of list, will iterate/recurse until done) |
| 276 | struct iter_context { |
| 277 | void (*fn)(intval_t); // output function |
| 278 | boolean accept_long_strings; // if FALSE, only 1-char-strings work |
| 279 | unsigned char stringxor; // for !scrxor, 0 otherwise |
| 280 | }; |
| 281 | extern void output_object(struct object *object, struct iter_context *iter); |
| 282 | |
| 283 | // output 8-bit value with range check |
| 284 | extern void output_8(intval_t value); |
| 285 | |
| 286 | // output 16-bit value with range check big-endian |
| 287 | extern void output_be16(intval_t value); |
| 288 | |
| 289 | // output 16-bit value with range check little-endian |
| 290 | extern void output_le16(intval_t value); |
| 291 | |
| 292 | // output 24-bit value with range check big-endian |
| 293 | extern void output_be24(intval_t value); |
| 294 | |
| 295 | // output 24-bit value with range check little-endian |
| 296 | extern void output_le24(intval_t value); |
| 297 | |
| 298 | // output 32-bit value (without range check) big-endian |
| 299 | extern void output_be32(intval_t value); |
| 300 | |
| 301 | // output 32-bit value (without range check) little-endian |
| 302 | extern void output_le32(intval_t value); |
| 303 | |
| 304 | |
| 305 | // string to show if outputformat_set() returns nonzero |
| 306 | extern const char outputformat_names[]; |
| 307 | |
| 308 | // convert output format name held in DynaBuf to enum. |
| 309 | // returns OUTFILE_FORMAT_UNSPECIFIED on error. |
| 310 | extern enum outfile_format outputformat_find(void); |
| 311 | |
| 312 | |
| 313 | #endif |