2016-12-28 20:32:00 +00:00
|
|
|
// ACME - a crossassembler for producing 6502/65c02/65816/65ce02 code.
|
2017-10-29 23:29:07 +00:00
|
|
|
// Copyright (C) 1998-2017 Marco Baye
|
2012-02-27 21:14:46 +00:00
|
|
|
//
|
|
|
|
// This program is free software; you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation; either version 2 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with this program; if not, write to the Free Software
|
|
|
|
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
2014-12-22 00:47:52 +00:00
|
|
|
#include "acme.h"
|
2012-02-27 21:14:46 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include "alu.h"
|
|
|
|
#include "cliargs.h"
|
|
|
|
#include "config.h"
|
|
|
|
#include "cpu.h"
|
|
|
|
#include "dynabuf.h"
|
|
|
|
#include "encoding.h"
|
|
|
|
#include "flow.h"
|
|
|
|
#include "global.h"
|
|
|
|
#include "input.h"
|
|
|
|
#include "macro.h"
|
|
|
|
#include "mnemo.h"
|
|
|
|
#include "output.h"
|
|
|
|
#include "platform.h"
|
2014-12-03 22:18:06 +00:00
|
|
|
#include "pseudoopcodes.h"
|
2012-02-27 21:14:46 +00:00
|
|
|
#include "section.h"
|
2014-06-07 00:12:10 +00:00
|
|
|
#include "symbol.h"
|
2016-07-19 08:11:58 +00:00
|
|
|
#include "version.h"
|
2012-02-27 21:14:46 +00:00
|
|
|
|
|
|
|
|
|
|
|
// constants
|
|
|
|
static const char FILE_WRITETEXT[] = "w";
|
|
|
|
static const char FILE_WRITEBINARY[] = "wb";
|
|
|
|
// names for error messages
|
|
|
|
static const char name_outfile[] = "output filename";
|
2014-11-22 01:36:02 +00:00
|
|
|
static const char arg_symbollist[] = "symbol list filename";
|
|
|
|
static const char arg_reportfile[] = "report filename";
|
2014-11-23 23:40:01 +00:00
|
|
|
static const char arg_vicelabels[] = "VICE labels filename";
|
2012-02-27 21:14:46 +00:00
|
|
|
// long options
|
|
|
|
#define OPTION_HELP "help"
|
|
|
|
#define OPTION_FORMAT "format"
|
|
|
|
#define OPTION_OUTFILE "outfile"
|
2014-11-22 01:36:02 +00:00
|
|
|
#define OPTION_LABELDUMP "labeldump" // old
|
|
|
|
#define OPTION_SYMBOLLIST "symbollist" // new
|
2014-11-23 23:40:01 +00:00
|
|
|
#define OPTION_VICELABELS "vicelabels"
|
2014-11-22 01:36:02 +00:00
|
|
|
#define OPTION_REPORT "report"
|
2012-02-27 21:14:46 +00:00
|
|
|
#define OPTION_SETPC "setpc"
|
|
|
|
#define OPTION_CPU "cpu"
|
|
|
|
#define OPTION_INITMEM "initmem"
|
|
|
|
#define OPTION_MAXERRORS "maxerrors"
|
|
|
|
#define OPTION_MAXDEPTH "maxdepth"
|
|
|
|
#define OPTION_USE_STDOUT "use-stdout"
|
|
|
|
#define OPTION_VERSION "version"
|
2014-11-23 23:40:01 +00:00
|
|
|
#define OPTION_MSVC "msvc"
|
2017-03-10 12:19:15 +00:00
|
|
|
#define OPTION_COLOR "color"
|
2017-10-29 23:29:07 +00:00
|
|
|
#define OPTION_FULLSTOP "fullstop"
|
2012-02-27 21:14:46 +00:00
|
|
|
// options for "-W"
|
|
|
|
#define OPTIONWNO_LABEL_INDENT "no-label-indent"
|
2014-06-02 00:47:46 +00:00
|
|
|
#define OPTIONWNO_OLD_FOR "no-old-for"
|
|
|
|
#define OPTIONWTYPE_MISMATCH "type-mismatch"
|
2012-02-27 21:14:46 +00:00
|
|
|
|
|
|
|
|
|
|
|
// variables
|
|
|
|
static const char **toplevel_sources;
|
|
|
|
static int toplevel_src_count = 0;
|
2014-03-10 00:17:10 +00:00
|
|
|
#define ILLEGAL_START_ADDRESS (-1)
|
|
|
|
static signed long start_address = ILLEGAL_START_ADDRESS;
|
2012-02-27 21:14:46 +00:00
|
|
|
static signed long fill_value = MEMINIT_USE_DEFAULT;
|
2014-03-10 00:17:10 +00:00
|
|
|
static const struct cpu_type *default_cpu = NULL;
|
2014-11-22 01:36:02 +00:00
|
|
|
const char *symbollist_filename = NULL;
|
2014-11-23 23:40:01 +00:00
|
|
|
const char *vicelabels_filename = NULL;
|
2012-02-27 21:14:46 +00:00
|
|
|
const char *output_filename = NULL;
|
2014-11-22 01:36:02 +00:00
|
|
|
const char *report_filename = NULL;
|
2012-02-27 21:14:46 +00:00
|
|
|
// maximum recursion depth for macro calls and "!source"
|
|
|
|
signed long macro_recursions_left = MAX_NESTING;
|
|
|
|
signed long source_recursions_left = MAX_NESTING;
|
|
|
|
|
|
|
|
|
|
|
|
// show release and platform info (and exit, if wanted)
|
|
|
|
static void show_version(int exit_after)
|
|
|
|
{
|
|
|
|
puts(
|
|
|
|
"This is ACME, release " RELEASE " (\"" CODENAME "\"), " CHANGE_DATE " " CHANGE_YEAR "\n"
|
|
|
|
" " PLATFORM_VERSION);
|
|
|
|
if (exit_after)
|
|
|
|
exit(EXIT_SUCCESS);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// show full help (headline, release/platform/version, copyright, dedication,
|
|
|
|
// warranty disclaimer, usage) and exit program (SUCCESS)
|
|
|
|
static void show_help_and_exit(void)
|
|
|
|
{
|
|
|
|
puts(
|
|
|
|
"ACME - the ACME Crossassembler for Multiple Environments\n"
|
|
|
|
" Copyright (C) 1998-" CHANGE_YEAR " Marco Baye");
|
|
|
|
show_version(FALSE);
|
|
|
|
puts(
|
|
|
|
"ACME comes with ABSOLUTELY NO WARRANTY; for details read the help file.\n"
|
|
|
|
" This is free software, and you are welcome to redistribute it under\n"
|
|
|
|
" certain conditions; as outlined in the GNU General Public License.\n"
|
|
|
|
"Dedicated to the wisest being I ever had the pleasure of reading\n"
|
|
|
|
" books of (currently spending some time dead for tax reasons).\n"
|
|
|
|
"The newest version can be found at the ACME homepage:\n"
|
|
|
|
" " HOME_PAGE "\n"
|
|
|
|
"\n"
|
|
|
|
"Usage:\n"
|
|
|
|
"acme [OPTION...] [FILE]...\n"
|
|
|
|
"\n"
|
|
|
|
"Options:\n"
|
|
|
|
" -h, --" OPTION_HELP " show this help and exit\n"
|
2014-11-22 01:36:02 +00:00
|
|
|
" -f, --" OPTION_FORMAT " FORMAT set output file format\n"
|
|
|
|
" -o, --" OPTION_OUTFILE " FILE set output file name\n"
|
|
|
|
" -r, --" OPTION_REPORT " FILE set report file name\n"
|
|
|
|
" -l, --" OPTION_SYMBOLLIST " FILE set symbol list file name\n"
|
|
|
|
" --" OPTION_LABELDUMP " (old name for --" OPTION_SYMBOLLIST ")\n"
|
2014-11-23 23:40:01 +00:00
|
|
|
" --" OPTION_VICELABELS " FILE set file name for label dump in VICE format\n"
|
2012-02-27 21:14:46 +00:00
|
|
|
" --" OPTION_SETPC " NUMBER set program counter\n"
|
2014-11-22 01:36:02 +00:00
|
|
|
" --" OPTION_CPU " CPU set target processor\n"
|
2012-02-27 21:14:46 +00:00
|
|
|
" --" OPTION_INITMEM " NUMBER define 'empty' memory\n"
|
|
|
|
" --" OPTION_MAXERRORS " NUMBER set number of errors before exiting\n"
|
|
|
|
" --" OPTION_MAXDEPTH " NUMBER set recursion depth for macro calls and !src\n"
|
|
|
|
" -vDIGIT set verbosity level\n"
|
2014-06-07 00:12:10 +00:00
|
|
|
" -DSYMBOL=VALUE define global symbol\n"
|
2017-12-22 22:55:36 +00:00
|
|
|
" -I PATH/TO/DIR add search path for input files\n"
|
2012-02-27 21:14:46 +00:00
|
|
|
// as long as there is only one -W option:
|
|
|
|
#define OPTIONWNO_LABEL_INDENT "no-label-indent"
|
|
|
|
" -W" OPTIONWNO_LABEL_INDENT " suppress warnings about indented labels\n"
|
2014-06-02 00:47:46 +00:00
|
|
|
" -W" OPTIONWNO_OLD_FOR " suppress warnings about old \"!for\" syntax\n"
|
|
|
|
" -W" OPTIONWTYPE_MISMATCH " enable type checking (warn about type mismatch)\n"
|
2012-02-27 21:14:46 +00:00
|
|
|
// when there are more, use next line and add a separate function:
|
|
|
|
//" -W show warning level options\n"
|
|
|
|
" --" OPTION_USE_STDOUT " fix for 'Relaunch64' IDE (see docs)\n"
|
2017-10-29 23:29:07 +00:00
|
|
|
" --" OPTION_MSVC " output errors in MS VS format\n"
|
|
|
|
" --" OPTION_COLOR " uses ANSI color codes for error output\n"
|
|
|
|
" --" OPTION_FULLSTOP " use '.' as pseudo opcode prefix\n"
|
2012-02-27 21:14:46 +00:00
|
|
|
PLATFORM_OPTION_HELP
|
|
|
|
" -V, --" OPTION_VERSION " show version and exit\n");
|
|
|
|
exit(EXIT_SUCCESS);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-11-22 01:36:02 +00:00
|
|
|
// initialise report struct
|
|
|
|
static void report_init(struct report *report)
|
|
|
|
{
|
|
|
|
report->fd = NULL;
|
|
|
|
report->asc_used = 0;
|
|
|
|
report->bin_used = 0;
|
|
|
|
report->last_input = NULL;
|
|
|
|
}
|
|
|
|
// open report file
|
|
|
|
static int report_open(struct report *report, const char *filename)
|
|
|
|
{
|
|
|
|
report->fd = fopen(filename, FILE_WRITETEXT);
|
|
|
|
if (report->fd == NULL) {
|
|
|
|
fprintf(stderr, "Error: Cannot open report file \"%s\".\n", filename);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0; // success
|
|
|
|
}
|
|
|
|
// close report file
|
|
|
|
static void report_close(struct report *report)
|
|
|
|
{
|
2015-02-05 18:29:01 +00:00
|
|
|
if (report && report->fd) {
|
2014-11-22 01:36:02 +00:00
|
|
|
fclose(report->fd);
|
|
|
|
report->fd = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-02-27 21:14:46 +00:00
|
|
|
// error handling
|
|
|
|
|
2014-11-22 01:36:02 +00:00
|
|
|
// tidy up before exiting by saving symbol list and close other output files
|
2012-02-27 21:14:46 +00:00
|
|
|
int ACME_finalize(int exit_code)
|
|
|
|
{
|
|
|
|
FILE *fd;
|
|
|
|
|
2014-11-22 01:36:02 +00:00
|
|
|
report_close(report);
|
|
|
|
if (symbollist_filename) {
|
2017-12-22 22:55:36 +00:00
|
|
|
fd = fopen(symbollist_filename, FILE_WRITETEXT); // FIXME - what if filename is given via !sl in sub-dir? fix path!
|
2012-02-27 21:14:46 +00:00
|
|
|
if (fd) {
|
2014-11-22 01:36:02 +00:00
|
|
|
symbols_list(fd);
|
2012-02-27 21:14:46 +00:00
|
|
|
fclose(fd);
|
2014-11-23 23:40:01 +00:00
|
|
|
PLATFORM_SETFILETYPE_TEXT(symbollist_filename);
|
2012-02-27 21:14:46 +00:00
|
|
|
} else {
|
2014-11-22 01:36:02 +00:00
|
|
|
fprintf(stderr, "Error: Cannot open symbol list file \"%s\".\n", symbollist_filename);
|
2012-02-27 21:14:46 +00:00
|
|
|
exit_code = EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
}
|
2014-11-23 23:40:01 +00:00
|
|
|
if (vicelabels_filename) {
|
|
|
|
fd = fopen(vicelabels_filename, FILE_WRITETEXT);
|
|
|
|
if (fd) {
|
|
|
|
symbols_vicelabels(fd);
|
|
|
|
fclose(fd);
|
|
|
|
PLATFORM_SETFILETYPE_TEXT(vicelabels_filename);
|
|
|
|
} else {
|
|
|
|
fprintf(stderr, "Error: Cannot open VICE label dump file \"%s\".\n", vicelabels_filename);
|
|
|
|
exit_code = EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
}
|
2012-02-27 21:14:46 +00:00
|
|
|
return exit_code;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// save output file
|
|
|
|
static void save_output_file(void)
|
|
|
|
{
|
|
|
|
FILE *fd;
|
|
|
|
|
|
|
|
// if no output file chosen, tell user and do nothing
|
|
|
|
if (output_filename == NULL) {
|
|
|
|
fputs("No output file specified (use the \"-o\" option or the \"!to\" pseudo opcode).\n", stderr);
|
|
|
|
return;
|
|
|
|
}
|
2017-12-22 22:55:36 +00:00
|
|
|
fd = fopen(output_filename, FILE_WRITEBINARY); // FIXME - what if filename is given via !to in sub-dir? fix path!
|
2012-02-27 21:14:46 +00:00
|
|
|
if (fd == NULL) {
|
|
|
|
fprintf(stderr, "Error: Cannot open output file \"%s\".\n",
|
|
|
|
output_filename);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
Output_save_file(fd);
|
|
|
|
fclose(fd);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// perform a single pass. Returns number of "NeedValue" type errors.
|
|
|
|
static int perform_pass(void)
|
|
|
|
{
|
|
|
|
FILE *fd;
|
2014-03-05 23:33:19 +00:00
|
|
|
int ii;
|
2012-02-27 21:14:46 +00:00
|
|
|
|
|
|
|
// call modules' "pass init" functions
|
2014-03-11 14:22:32 +00:00
|
|
|
Output_passinit(); // disable output, PC undefined
|
2014-12-16 08:21:44 +00:00
|
|
|
cputype_passinit(default_cpu); // set default cpu type
|
2014-03-10 00:17:10 +00:00
|
|
|
// if start address was given on command line, use it:
|
|
|
|
if (start_address != ILLEGAL_START_ADDRESS)
|
2014-03-11 14:22:32 +00:00
|
|
|
vcpu_set_pc(start_address, 0);
|
2014-12-04 23:58:00 +00:00
|
|
|
encoding_passinit(); // set default encoding
|
2016-08-05 09:59:07 +00:00
|
|
|
section_passinit(); // set initial zone (untitled)
|
2012-02-27 21:14:46 +00:00
|
|
|
// init variables
|
|
|
|
pass_undefined_count = 0; // no "NeedValue" errors yet
|
|
|
|
pass_real_errors = 0; // no real errors yet
|
|
|
|
// Process toplevel files
|
2014-11-30 17:00:13 +00:00
|
|
|
for (ii = 0; ii < toplevel_src_count; ++ii) {
|
2014-03-05 23:33:19 +00:00
|
|
|
if ((fd = fopen(toplevel_sources[ii], FILE_READBINARY))) {
|
2014-12-22 00:47:52 +00:00
|
|
|
flow_parse_and_close_file(fd, toplevel_sources[ii]);
|
2012-02-27 21:14:46 +00:00
|
|
|
} else {
|
2014-03-05 23:33:19 +00:00
|
|
|
fprintf(stderr, "Error: Cannot open toplevel file \"%s\".\n", toplevel_sources[ii]);
|
2016-02-21 12:58:22 +00:00
|
|
|
if (toplevel_sources[ii][0] == '-')
|
|
|
|
fprintf(stderr, "Options (starting with '-') must be given _before_ source files!\n");
|
2014-11-30 17:00:13 +00:00
|
|
|
++pass_real_errors;
|
2012-02-27 21:14:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (pass_real_errors)
|
|
|
|
exit(ACME_finalize(EXIT_FAILURE));
|
|
|
|
else
|
|
|
|
Output_end_segment();
|
|
|
|
return pass_undefined_count;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-02-05 18:29:01 +00:00
|
|
|
static struct report global_report;
|
|
|
|
// do passes until done (or errors occurred). Return whether output is ready.
|
2012-02-27 21:14:46 +00:00
|
|
|
static int do_actual_work(void)
|
|
|
|
{
|
2014-11-22 01:36:02 +00:00
|
|
|
int undefined_prev, // "NeedValue" errors of previous pass
|
|
|
|
undefined_curr; // "NeedValue" errors of current pass
|
2012-02-27 21:14:46 +00:00
|
|
|
|
2015-02-05 18:29:01 +00:00
|
|
|
report = &global_report; // let global pointer point to something
|
2014-11-22 01:36:02 +00:00
|
|
|
report_init(report); // we must init struct before doing passes
|
2017-10-29 23:29:07 +00:00
|
|
|
if (config.process_verbosity > 1)
|
2012-02-27 21:14:46 +00:00
|
|
|
puts("First pass.");
|
|
|
|
pass_count = 0;
|
|
|
|
undefined_curr = perform_pass(); // First pass
|
|
|
|
// now pretend there has been a pass before the first one
|
|
|
|
undefined_prev = undefined_curr + 1;
|
|
|
|
// As long as the number of "NeedValue" errors is decreasing but
|
|
|
|
// non-zero, keep doing passes.
|
|
|
|
while (undefined_curr && (undefined_curr < undefined_prev)) {
|
2014-11-22 01:36:02 +00:00
|
|
|
++pass_count;
|
2012-02-27 21:14:46 +00:00
|
|
|
undefined_prev = undefined_curr;
|
2017-10-29 23:29:07 +00:00
|
|
|
if (config.process_verbosity > 1)
|
2012-02-27 21:14:46 +00:00
|
|
|
puts("Further pass.");
|
|
|
|
undefined_curr = perform_pass();
|
|
|
|
}
|
2014-11-22 01:36:02 +00:00
|
|
|
// any errors left?
|
|
|
|
if (undefined_curr == 0) {
|
|
|
|
// if listing report is wanted and there were no errors,
|
|
|
|
// do another pass to generate listing report
|
|
|
|
if (report_filename) {
|
2017-10-29 23:29:07 +00:00
|
|
|
if (config.process_verbosity > 1)
|
2014-11-22 01:36:02 +00:00
|
|
|
puts("Extra pass to generate listing report.");
|
|
|
|
if (report_open(report, report_filename) == 0) {
|
|
|
|
++pass_count;
|
|
|
|
perform_pass();
|
|
|
|
report_close(report);
|
|
|
|
}
|
|
|
|
}
|
2012-02-27 21:14:46 +00:00
|
|
|
return 1;
|
2014-11-22 01:36:02 +00:00
|
|
|
}
|
|
|
|
// There are still errors (unsolvable by doing further passes),
|
|
|
|
// so perform additional pass to find and show them.
|
2017-10-29 23:29:07 +00:00
|
|
|
if (config.process_verbosity > 1)
|
2014-11-22 01:36:02 +00:00
|
|
|
puts("Extra pass needed to find error.");
|
2015-06-14 23:16:23 +00:00
|
|
|
// activate error output
|
|
|
|
ALU_optional_notdef_handler = Throw_error;
|
|
|
|
|
2014-11-22 01:36:02 +00:00
|
|
|
++pass_count;
|
2012-02-27 21:14:46 +00:00
|
|
|
perform_pass(); // perform pass, but now show "value undefined"
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// copy string to DynaBuf
|
|
|
|
static void keyword_to_dynabuf(const char keyword[])
|
|
|
|
{
|
|
|
|
DYNABUF_CLEAR(GlobalDynaBuf);
|
|
|
|
DynaBuf_add_string(GlobalDynaBuf, keyword);
|
|
|
|
DynaBuf_append(GlobalDynaBuf, '\0');
|
|
|
|
DynaBuf_to_lower(GlobalDynaBuf, GlobalDynaBuf); // convert to lower case
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// check output format (the output format tree must be set up at this point!)
|
|
|
|
static void set_output_format(void)
|
|
|
|
{
|
|
|
|
keyword_to_dynabuf(cliargs_safe_get_next("output format"));
|
2016-02-21 12:58:22 +00:00
|
|
|
if (outputfile_set_format()) {
|
|
|
|
fprintf(stderr, "%sUnknown output format (known formats are: %s).\n", cliargs_error, outputfile_formats);
|
2012-02-27 21:14:46 +00:00
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// check CPU type (the cpu type tree must be set up at this point!)
|
|
|
|
static void set_starting_cpu(void)
|
|
|
|
{
|
2014-12-04 15:31:54 +00:00
|
|
|
const struct cpu_type *new_cpu_type;
|
|
|
|
|
2012-02-27 21:14:46 +00:00
|
|
|
keyword_to_dynabuf(cliargs_safe_get_next("CPU type"));
|
2014-12-04 15:31:54 +00:00
|
|
|
new_cpu_type = cputype_find();
|
|
|
|
if (new_cpu_type) {
|
|
|
|
default_cpu = new_cpu_type;
|
|
|
|
} else {
|
2016-02-21 12:58:22 +00:00
|
|
|
fprintf(stderr, "%sUnknown CPU type (known types are: %s).\n", cliargs_error, cputype_names);
|
2012-02-27 21:14:46 +00:00
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void could_not_parse(const char strange[])
|
|
|
|
{
|
|
|
|
fprintf(stderr, "%sCould not parse '%s'.\n", cliargs_error, strange);
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// return signed long representation of string.
|
|
|
|
// copes with hexadecimal if prefixed with "$", "0x" or "0X".
|
|
|
|
// copes with octal if prefixed with "&".
|
|
|
|
// copes with binary if prefixed with "%".
|
|
|
|
// assumes decimal otherwise.
|
|
|
|
static signed long string_to_number(const char *string)
|
|
|
|
{
|
|
|
|
signed long result;
|
|
|
|
char *end;
|
|
|
|
int base = 10;
|
|
|
|
|
|
|
|
if (*string == '%') {
|
|
|
|
base = 2;
|
2014-11-30 17:00:13 +00:00
|
|
|
++string;
|
2012-02-27 21:14:46 +00:00
|
|
|
} else if (*string == '&') {
|
|
|
|
base = 8;
|
2014-11-30 17:00:13 +00:00
|
|
|
++string;
|
2012-02-27 21:14:46 +00:00
|
|
|
} else if (*string == '$') {
|
|
|
|
base = 16;
|
2014-11-30 17:00:13 +00:00
|
|
|
++string;
|
2012-02-27 21:14:46 +00:00
|
|
|
} else if ((*string == '0') && ((string[1] == 'x') || (string[1] == 'X'))) {
|
|
|
|
base = 16;
|
|
|
|
string += 2;
|
|
|
|
}
|
|
|
|
result = strtol(string, &end, base);
|
|
|
|
if (*end)
|
|
|
|
could_not_parse(end);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// set program counter
|
|
|
|
static void set_starting_pc(void)
|
|
|
|
{
|
2014-03-10 00:17:10 +00:00
|
|
|
start_address = string_to_number(cliargs_safe_get_next("program counter"));
|
|
|
|
if ((start_address > -1) && (start_address < 65536))
|
2012-02-27 21:14:46 +00:00
|
|
|
return;
|
|
|
|
fprintf(stderr, "%sProgram counter out of range (0-0xffff).\n", cliargs_error);
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// set initial memory contents
|
|
|
|
static void set_mem_contents(void)
|
|
|
|
{
|
|
|
|
fill_value = string_to_number(cliargs_safe_get_next("initmem value"));
|
|
|
|
if ((fill_value >= -128) && (fill_value <= 255))
|
|
|
|
return;
|
|
|
|
fprintf(stderr, "%sInitmem value out of range (0-0xff).\n", cliargs_error);
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-06-07 00:12:10 +00:00
|
|
|
// define symbol
|
|
|
|
static void define_symbol(const char definition[])
|
2012-02-27 21:14:46 +00:00
|
|
|
{
|
|
|
|
const char *walk = definition;
|
|
|
|
signed long value;
|
|
|
|
|
|
|
|
// copy definition to GlobalDynaBuf until '=' reached
|
|
|
|
DYNABUF_CLEAR(GlobalDynaBuf);
|
|
|
|
while ((*walk != '=') && (*walk != '\0'))
|
|
|
|
DynaBuf_append(GlobalDynaBuf, *walk++);
|
|
|
|
if ((*walk == '\0') || (walk[1] == '\0'))
|
|
|
|
could_not_parse(definition);
|
|
|
|
value = string_to_number(walk + 1);
|
|
|
|
DynaBuf_append(GlobalDynaBuf, '\0');
|
2014-06-07 00:12:10 +00:00
|
|
|
symbol_define(value);
|
2012-02-27 21:14:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// handle long options (like "--example"). Return unknown string.
|
|
|
|
static const char *long_option(const char *string)
|
|
|
|
{
|
|
|
|
if (strcmp(string, OPTION_HELP) == 0)
|
|
|
|
show_help_and_exit();
|
|
|
|
else if (strcmp(string, OPTION_FORMAT) == 0)
|
|
|
|
set_output_format();
|
|
|
|
else if (strcmp(string, OPTION_OUTFILE) == 0)
|
|
|
|
output_filename = cliargs_safe_get_next(name_outfile);
|
2014-11-22 01:36:02 +00:00
|
|
|
else if (strcmp(string, OPTION_LABELDUMP) == 0) // old
|
|
|
|
symbollist_filename = cliargs_safe_get_next(arg_symbollist);
|
|
|
|
else if (strcmp(string, OPTION_SYMBOLLIST) == 0) // new
|
|
|
|
symbollist_filename = cliargs_safe_get_next(arg_symbollist);
|
2014-11-23 23:40:01 +00:00
|
|
|
else if (strcmp(string, OPTION_VICELABELS) == 0)
|
|
|
|
vicelabels_filename = cliargs_safe_get_next(arg_vicelabels);
|
2014-11-22 01:36:02 +00:00
|
|
|
else if (strcmp(string, OPTION_REPORT) == 0)
|
|
|
|
report_filename = cliargs_safe_get_next(arg_reportfile);
|
2012-02-27 21:14:46 +00:00
|
|
|
else if (strcmp(string, OPTION_SETPC) == 0)
|
|
|
|
set_starting_pc();
|
|
|
|
else if (strcmp(string, OPTION_CPU) == 0)
|
|
|
|
set_starting_cpu();
|
|
|
|
else if (strcmp(string, OPTION_INITMEM) == 0)
|
|
|
|
set_mem_contents();
|
|
|
|
else if (strcmp(string, OPTION_MAXERRORS) == 0)
|
2017-10-29 23:29:07 +00:00
|
|
|
config.max_errors = string_to_number(cliargs_safe_get_next("maximum error count"));
|
2012-02-27 21:14:46 +00:00
|
|
|
else if (strcmp(string, OPTION_MAXDEPTH) == 0)
|
2014-11-22 01:36:02 +00:00
|
|
|
macro_recursions_left = (source_recursions_left = string_to_number(cliargs_safe_get_next("recursion depth")));
|
2012-02-27 21:14:46 +00:00
|
|
|
// else if (strcmp(string, "strictsyntax") == 0)
|
|
|
|
// strict_syntax = TRUE;
|
|
|
|
else if (strcmp(string, OPTION_USE_STDOUT) == 0)
|
|
|
|
msg_stream = stdout;
|
2014-11-23 23:40:01 +00:00
|
|
|
else if (strcmp(string, OPTION_MSVC) == 0)
|
2017-10-29 23:29:07 +00:00
|
|
|
config.format_msvc = TRUE;
|
|
|
|
else if (strcmp(string, OPTION_FULLSTOP) == 0)
|
|
|
|
config.pseudoop_prefix = '.';
|
2012-02-27 21:14:46 +00:00
|
|
|
PLATFORM_LONGOPTION_CODE
|
2017-03-10 12:19:15 +00:00
|
|
|
else if (strcmp(string, OPTION_COLOR) == 0)
|
2017-10-29 23:29:07 +00:00
|
|
|
config.format_color = TRUE;
|
2012-02-27 21:14:46 +00:00
|
|
|
else if (strcmp(string, OPTION_VERSION) == 0)
|
|
|
|
show_version(TRUE);
|
2014-11-22 01:36:02 +00:00
|
|
|
else
|
|
|
|
return string;
|
2012-02-27 21:14:46 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// handle short options (like "-e"). Return unknown character.
|
|
|
|
static char short_option(const char *argument)
|
|
|
|
{
|
|
|
|
while (*argument) {
|
|
|
|
switch (*argument) {
|
|
|
|
case 'D': // "-D" define constants
|
2014-06-07 00:12:10 +00:00
|
|
|
define_symbol(argument + 1);
|
2012-02-27 21:14:46 +00:00
|
|
|
goto done;
|
|
|
|
case 'f': // "-f" selects output format
|
|
|
|
set_output_format();
|
|
|
|
break;
|
2017-12-22 22:55:36 +00:00
|
|
|
case 'h': // "-h" shows help
|
|
|
|
show_help_and_exit();
|
2012-02-27 21:14:46 +00:00
|
|
|
break;
|
2017-12-22 22:55:36 +00:00
|
|
|
case 'I': // "-I" adds an include directory
|
|
|
|
if (argument[1])
|
|
|
|
includepaths_add(argument + 1);
|
|
|
|
else
|
|
|
|
includepaths_add(cliargs_safe_get_next("include path"));
|
|
|
|
goto done;
|
2014-11-22 01:36:02 +00:00
|
|
|
case 'l': // "-l" selects symbol list filename
|
|
|
|
symbollist_filename = cliargs_safe_get_next(arg_symbollist);
|
|
|
|
break;
|
2017-12-22 22:55:36 +00:00
|
|
|
case 'o': // "-o" selects output filename
|
|
|
|
output_filename = cliargs_safe_get_next(name_outfile);
|
|
|
|
break;
|
2014-11-22 01:36:02 +00:00
|
|
|
case 'r': // "-r" selects report filename
|
|
|
|
report_filename = cliargs_safe_get_next(arg_reportfile);
|
2012-02-27 21:14:46 +00:00
|
|
|
break;
|
|
|
|
case 'v': // "-v" changes verbosity
|
2017-10-29 23:29:07 +00:00
|
|
|
++config.process_verbosity;
|
2012-02-27 21:14:46 +00:00
|
|
|
if ((argument[1] >= '0') && (argument[1] <= '9'))
|
2017-10-29 23:29:07 +00:00
|
|
|
config.process_verbosity = *(++argument) - '0';
|
2012-02-27 21:14:46 +00:00
|
|
|
break;
|
|
|
|
// platform specific switches are inserted here
|
|
|
|
PLATFORM_SHORTOPTION_CODE
|
|
|
|
case 'V': // "-V" shows version
|
|
|
|
show_version(TRUE);
|
|
|
|
break;
|
|
|
|
case 'W': // "-W" tunes warning level
|
|
|
|
if (strcmp(argument + 1, OPTIONWNO_LABEL_INDENT) == 0) {
|
2017-10-29 23:29:07 +00:00
|
|
|
config.warn_on_indented_labels = FALSE;
|
2012-02-27 21:14:46 +00:00
|
|
|
goto done;
|
2014-06-02 00:47:46 +00:00
|
|
|
} else if (strcmp(argument + 1, OPTIONWNO_OLD_FOR) == 0) {
|
2017-10-29 23:29:07 +00:00
|
|
|
config.warn_on_old_for = FALSE;
|
2014-06-02 00:47:46 +00:00
|
|
|
goto done;
|
|
|
|
} else if (strcmp(argument + 1, OPTIONWTYPE_MISMATCH) == 0) {
|
2017-10-29 23:29:07 +00:00
|
|
|
config.warn_on_type_mismatch = TRUE;
|
2014-06-02 00:47:46 +00:00
|
|
|
goto done;
|
2012-02-27 21:14:46 +00:00
|
|
|
} else {
|
|
|
|
fprintf(stderr, "%sUnknown warning level.\n", cliargs_error);
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default: // unknown ones: program termination
|
|
|
|
return *argument;
|
|
|
|
}
|
2014-11-30 17:00:13 +00:00
|
|
|
++argument;
|
2012-02-27 21:14:46 +00:00
|
|
|
}
|
|
|
|
done:
|
|
|
|
return '\0';
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// guess what
|
|
|
|
int main(int argc, const char *argv[])
|
|
|
|
{
|
2017-10-29 23:29:07 +00:00
|
|
|
config_default(&config);
|
2012-02-27 21:14:46 +00:00
|
|
|
// if called without any arguments, show usage info (not full help)
|
|
|
|
if (argc == 1)
|
|
|
|
show_help_and_exit();
|
|
|
|
msg_stream = stderr;
|
|
|
|
cliargs_init(argc, argv);
|
|
|
|
DynaBuf_init(); // inits *global* dynamic buffer - important, so first
|
|
|
|
// Init platform-specific stuff.
|
|
|
|
// For example, this could read the library path from an
|
|
|
|
// environment variable, which in turn may need DynaBuf already.
|
|
|
|
PLATFORM_INIT;
|
|
|
|
// prepare a buffer large enough to hold pointers to "-D" switch values
|
|
|
|
// cli_defines = safe_malloc(argc * sizeof(*cli_defines));
|
2017-12-22 22:55:36 +00:00
|
|
|
includepaths_init(); // must be done before cli arg handling
|
2012-02-27 21:14:46 +00:00
|
|
|
// handle command line arguments
|
|
|
|
cliargs_handle_options(short_option, long_option);
|
|
|
|
// generate list of files to process
|
2014-11-22 01:36:02 +00:00
|
|
|
cliargs_get_rest(&toplevel_src_count, &toplevel_sources, "No top level sources given");
|
2012-02-27 21:14:46 +00:00
|
|
|
// Init modules (most of them will just build keyword trees)
|
|
|
|
ALU_init();
|
|
|
|
Macro_init();
|
|
|
|
Mnemo_init();
|
|
|
|
Output_init(fill_value);
|
2014-12-03 22:18:06 +00:00
|
|
|
pseudoopcodes_init(); // setup keyword tree for pseudo opcodes
|
2012-02-27 21:14:46 +00:00
|
|
|
if (do_actual_work())
|
|
|
|
save_output_file();
|
|
|
|
return ACME_finalize(EXIT_SUCCESS); // dump labels, if wanted
|
|
|
|
}
|