| 1 |
// |
| 2 |
// ACME - a crossassembler for producing 6502/65c02/65816 code. |
| 3 |
// Copyright (C) 1998-2006 Marco Baye |
| 4 |
// |
| 5 |
// Modified by abaddon 2013 |
| 6 |
// |
| 7 |
// This program is free software; you can redistribute it and/or modify |
| 8 |
// it under the terms of the GNU General Public License as published by |
| 9 |
// the Free Software Foundation; either version 2 of the License, or |
| 10 |
// (at your option) any later version. |
| 11 |
// |
| 12 |
// This program is distributed in the hope that it will be useful, |
| 13 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 |
// GNU General Public License for more details. |
| 16 |
// |
| 17 |
// You should have received a copy of the GNU General Public License |
| 18 |
// along with this program; if not, write to the Free Software |
| 19 |
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 20 |
|
| 21 |
#define RELEASE "0.93" // update before release (FIXME) |
| 22 |
#define CODENAME "Zarquon" // update before release |
| 23 |
#define CHANGE_DATE "11 Oct" // update before release |
| 24 |
#define CHANGE_YEAR "2006" // update before release |
| 25 |
#define HOME_PAGE "http://home.pages.de/~mac_bacon/smorbrod/acme/" |
| 26 |
|
| 27 |
#include <stdio.h> |
| 28 |
#include <stdlib.h> |
| 29 |
#include <string.h> |
| 30 |
#include <setjmp.h> |
| 31 |
#include "acme.h" |
| 32 |
#include "alu.h" |
| 33 |
#include "basics.h" |
| 34 |
#include "config.h" |
| 35 |
#include "cpu.h" |
| 36 |
#include "dynabuf.h" |
| 37 |
#include "encoding.h" |
| 38 |
#include "flow.h" |
| 39 |
#include "global.h" |
| 40 |
#include "input.h" |
| 41 |
#include "label.h" |
| 42 |
#include "macro.h" |
| 43 |
#include "mnemo.h" |
| 44 |
#include "output.h" |
| 45 |
#include "platform.h" |
| 46 |
#include "section.h" |
| 47 |
|
| 48 |
|
| 49 |
// Constants |
| 50 |
static const char FILE_WRITETEXT[] = "w"; |
| 51 |
static const char FILE_WRITEBINARY[] = "wb"; |
| 52 |
// names for error messages |
| 53 |
static const char name_outfile[] = "output filename"; |
| 54 |
static const char name_dumpfile[] = "label dump filename"; |
| 55 |
// long options |
| 56 |
#define OPTION_HELP "help" |
| 57 |
#define OPTION_FORMAT "format" |
| 58 |
#define OPTION_OUTFILE "outfile" |
| 59 |
#define OPTION_LABELDUMP "labeldump" |
| 60 |
#define OPTION_SETPC "setpc" |
| 61 |
#define OPTION_CPU "cpu" |
| 62 |
#define OPTION_INITMEM "initmem" |
| 63 |
#define OPTION_MAXERRORS "maxerrors" |
| 64 |
#define OPTION_MAXDEPTH "maxdepth" |
| 65 |
#define OPTION_USE_STDOUT "use-stdout" |
| 66 |
#define OPTION_VERSION "version" |
| 67 |
|
| 68 |
|
| 69 |
// Variables |
| 70 |
static signed long start_addr = -1; // <0 is illegal |
| 71 |
static signed long fill_value = MEMINIT_USE_DEFAULT; |
| 72 |
static struct cpu_t *default_cpu = NULL; |
| 73 |
const char* labeldump_filename = NULL; |
| 74 |
const char* output_filename = NULL; |
| 75 |
const char* source = NULL; |
| 76 |
// maximum recursion depth for macro calls and "!source" |
| 77 |
signed long macro_recursions_left = MAX_NESTING; |
| 78 |
signed long source_recursions_left = MAX_NESTING; |
| 79 |
|
| 80 |
|
| 81 |
// Perform a single pass. Returns number of "NeedValue" type errors. |
| 82 |
static int perform_pass(void) { |
| 83 |
// call modules' "pass init" functions |
| 84 |
CPU_passinit(default_cpu);// set default cpu values (PC undefined) |
| 85 |
Output_passinit(start_addr);// call after CPU_passinit(), to define PC |
| 86 |
Encoding_passinit(); // set default encoding |
| 87 |
Section_passinit(); // set initial zone (untitled) |
| 88 |
// init variables |
| 89 |
pass_undefined_count = 0; // no "NeedValue" errors yet |
| 90 |
pass_real_errors = 0; // no real errors yet |
| 91 |
Parse_source(source); |
| 92 |
if(pass_real_errors) { |
| 93 |
longjmp(exception_env, 0); |
| 94 |
} else |
| 95 |
Output_end_segment(); |
| 96 |
return(pass_undefined_count); |
| 97 |
} |
| 98 |
|
| 99 |
// do passes until done (or errors occured). Return whether output is ready. |
| 100 |
static bool do_actual_work(void) { |
| 101 |
int undefined_prev, // "NeedValue" errors of previous pass |
| 102 |
undefined_curr; // "NeedValue" errors of current pass |
| 103 |
|
| 104 |
if(Process_verbosity > 1) |
| 105 |
puts("First pass."); |
| 106 |
pass_count = 0; |
| 107 |
undefined_curr = perform_pass(); // First pass |
| 108 |
// now pretend there has been a pass before the first one |
| 109 |
undefined_prev = undefined_curr + 1; |
| 110 |
// As long as the number of "NeedValue" errors is decreasing but |
| 111 |
// non-zero, keep doing passes. |
| 112 |
while(undefined_curr && (undefined_curr < undefined_prev)) { |
| 113 |
pass_count++; |
| 114 |
undefined_prev = undefined_curr; |
| 115 |
if(Process_verbosity > 1) |
| 116 |
puts("Further pass."); |
| 117 |
undefined_curr = perform_pass(); |
| 118 |
} |
| 119 |
// If still errors (unsolvable by doing further passes), |
| 120 |
// perform additional pass to find and show them |
| 121 |
if(undefined_curr == 0) |
| 122 |
return(TRUE); |
| 123 |
if(Process_verbosity > 1) |
| 124 |
puts("Further pass needed to find error."); |
| 125 |
ALU_throw_errors(); // activate error output (CAUTION - one-way!) |
| 126 |
pass_count++; |
| 127 |
perform_pass(); // perform pass, but now show "value undefined" |
| 128 |
return(FALSE); |
| 129 |
} |
| 130 |
|
| 131 |
char* acme_assemble(const char* src, const int *length, char *error) { |
| 132 |
error_message = error; |
| 133 |
source = src; |
| 134 |
msg_stream = stdout; |
| 135 |
DynaBuf_init();// inits *global* dynamic buffer - important, so first |
| 136 |
// Init platform-specific stuff. |
| 137 |
// For example, this could read the library path from an |
| 138 |
// environment variable, which in turn may need DynaBuf already. |
| 139 |
PLATFORM_INIT; |
| 140 |
// init some keyword trees needed for argument handling |
| 141 |
CPUtype_init(); |
| 142 |
Outputfile_init(); |
| 143 |
// Init modules (most of them will just build keyword trees) |
| 144 |
ALU_init(); |
| 145 |
Basics_init(); |
| 146 |
CPU_init(); |
| 147 |
Encoding_init(); |
| 148 |
Flow_init(); |
| 149 |
Input_init(); |
| 150 |
Label_init(); |
| 151 |
Macro_init(); |
| 152 |
Mnemo_init(); |
| 153 |
Output_init(fill_value); |
| 154 |
Section_init(); |
| 155 |
if(!setjmp(exception_env)) { |
| 156 |
if(do_actual_work()) { |
| 157 |
return Output_get_final_data(length); |
| 158 |
} |
| 159 |
} |
| 160 |
return NULL; |
| 161 |
} |