2000-05-28 13:40:48 +00:00
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
/* */
|
|
|
|
|
/* main.c */
|
|
|
|
|
/* */
|
|
|
|
|
/* Main program for the ca65 macroassembler */
|
|
|
|
|
/* */
|
|
|
|
|
/* */
|
|
|
|
|
/* */
|
2003-05-25 17:57:50 +00:00
|
|
|
|
/* (C) 1998-2003 Ullrich von Bassewitz */
|
2003-11-08 17:20:21 +00:00
|
|
|
|
/* R<>merstra<72>e 52 */
|
2003-05-25 17:57:50 +00:00
|
|
|
|
/* D-70794 Filderstadt */
|
|
|
|
|
/* EMail: uz@cc65.org */
|
2000-05-28 13:40:48 +00:00
|
|
|
|
/* */
|
|
|
|
|
/* */
|
|
|
|
|
/* This software is provided 'as-is', without any expressed or implied */
|
|
|
|
|
/* warranty. In no event will the authors be held liable for any damages */
|
|
|
|
|
/* arising from the use of this software. */
|
|
|
|
|
/* */
|
|
|
|
|
/* Permission is granted to anyone to use this software for any purpose, */
|
|
|
|
|
/* including commercial applications, and to alter it and redistribute it */
|
|
|
|
|
/* freely, subject to the following restrictions: */
|
|
|
|
|
/* */
|
|
|
|
|
/* 1. The origin of this software must not be misrepresented; you must not */
|
|
|
|
|
/* claim that you wrote the original software. If you use this software */
|
|
|
|
|
/* in a product, an acknowledgment in the product documentation would be */
|
|
|
|
|
/* appreciated but is not required. */
|
|
|
|
|
/* 2. Altered source versions must be plainly marked as such, and must not */
|
|
|
|
|
/* be misrepresented as being the original software. */
|
|
|
|
|
/* 3. This notice may not be removed or altered from any source */
|
|
|
|
|
/* distribution. */
|
|
|
|
|
/* */
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
#include <time.h>
|
|
|
|
|
|
2000-07-28 12:15:40 +00:00
|
|
|
|
/* common */
|
2003-11-07 19:28:37 +00:00
|
|
|
|
#include "addrsize.h"
|
2001-01-05 19:24:47 +00:00
|
|
|
|
#include "chartype.h"
|
2000-07-28 12:15:40 +00:00
|
|
|
|
#include "cmdline.h"
|
2003-11-28 22:12:14 +00:00
|
|
|
|
#include "mmodel.h"
|
2001-03-10 10:21:03 +00:00
|
|
|
|
#include "print.h"
|
2000-08-23 07:01:18 +00:00
|
|
|
|
#include "target.h"
|
2000-09-14 19:26:13 +00:00
|
|
|
|
#include "tgttrans.h"
|
2000-07-28 12:15:40 +00:00
|
|
|
|
#include "version.h"
|
2000-08-01 21:36:45 +00:00
|
|
|
|
|
2000-07-28 12:15:40 +00:00
|
|
|
|
/* ca65 */
|
2000-08-21 21:20:40 +00:00
|
|
|
|
#include "abend.h"
|
2003-06-06 20:47:59 +00:00
|
|
|
|
#include "asserts.h"
|
2000-05-28 13:40:48 +00:00
|
|
|
|
#include "error.h"
|
|
|
|
|
#include "expr.h"
|
2000-09-02 11:35:22 +00:00
|
|
|
|
#include "feature.h"
|
2000-08-01 21:36:45 +00:00
|
|
|
|
#include "filetab.h"
|
2000-05-28 13:40:48 +00:00
|
|
|
|
#include "global.h"
|
2000-05-29 18:40:56 +00:00
|
|
|
|
#include "incpath.h"
|
2000-05-28 13:40:48 +00:00
|
|
|
|
#include "instr.h"
|
2000-07-09 13:19:25 +00:00
|
|
|
|
#include "istack.h"
|
2001-05-23 19:03:40 +00:00
|
|
|
|
#include "lineinfo.h"
|
2000-05-28 13:40:48 +00:00
|
|
|
|
#include "listing.h"
|
|
|
|
|
#include "macro.h"
|
2000-06-03 11:15:11 +00:00
|
|
|
|
#include "nexttok.h"
|
2000-05-28 13:40:48 +00:00
|
|
|
|
#include "objfile.h"
|
|
|
|
|
#include "options.h"
|
|
|
|
|
#include "pseudo.h"
|
|
|
|
|
#include "scanner.h"
|
2003-10-21 20:34:56 +00:00
|
|
|
|
#include "segment.h"
|
2003-05-25 17:57:50 +00:00
|
|
|
|
#include "spool.h"
|
2000-05-28 13:40:48 +00:00
|
|
|
|
#include "symtab.h"
|
|
|
|
|
#include "ulabel.h"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
/* Code */
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static void Usage (void)
|
|
|
|
|
/* Print usage information and exit */
|
|
|
|
|
{
|
|
|
|
|
fprintf (stderr,
|
|
|
|
|
"Usage: %s [options] file\n"
|
2000-05-29 18:40:56 +00:00
|
|
|
|
"Short options:\n"
|
2001-10-02 19:01:00 +00:00
|
|
|
|
" -D name[=value]\tDefine a symbol\n"
|
|
|
|
|
" -I dir\t\tSet an include directory search path\n"
|
|
|
|
|
" -U\t\t\tMark unresolved symbols as import\n"
|
|
|
|
|
" -V\t\t\tPrint the assembler version\n"
|
|
|
|
|
" -W n\t\t\tSet warning level n\n"
|
2000-05-29 18:40:56 +00:00
|
|
|
|
" -g\t\t\tAdd debug info to object file\n"
|
2000-07-08 21:01:02 +00:00
|
|
|
|
" -h\t\t\tHelp (this text)\n"
|
2000-05-29 18:40:56 +00:00
|
|
|
|
" -i\t\t\tIgnore case of symbols\n"
|
|
|
|
|
" -l\t\t\tCreate a listing if assembly was ok\n"
|
2003-11-23 21:38:23 +00:00
|
|
|
|
" -mm model\t\tSet the memory model\n"
|
2000-05-29 18:40:56 +00:00
|
|
|
|
" -o name\t\tName the output file\n"
|
|
|
|
|
" -s\t\t\tEnable smart mode\n"
|
2000-08-21 21:20:40 +00:00
|
|
|
|
" -t sys\t\tSet the target system\n"
|
2000-05-29 18:40:56 +00:00
|
|
|
|
" -v\t\t\tIncrease verbosity\n"
|
|
|
|
|
"\n"
|
|
|
|
|
"Long options:\n"
|
|
|
|
|
" --auto-import\t\tMark unresolved symbols as import\n"
|
|
|
|
|
" --cpu type\t\tSet cpu type\n"
|
|
|
|
|
" --debug-info\t\tAdd debug info to object file\n"
|
2000-09-02 11:35:22 +00:00
|
|
|
|
" --feature name\tSet an emulation feature\n"
|
2000-06-14 09:32:22 +00:00
|
|
|
|
" --help\t\tHelp (this text)\n"
|
2000-05-29 18:40:56 +00:00
|
|
|
|
" --ignore-case\t\tIgnore case of symbols\n"
|
|
|
|
|
" --include-dir dir\tSet an include directory search path\n"
|
|
|
|
|
" --listing\t\tCreate a listing if assembly was ok\n"
|
2003-11-23 21:38:23 +00:00
|
|
|
|
" --memory-model model\tSet the memory model\n"
|
2000-05-29 18:40:56 +00:00
|
|
|
|
" --pagelength n\tSet the page length for the listing\n"
|
|
|
|
|
" --smart\t\tEnable smart mode\n"
|
2000-08-21 21:20:40 +00:00
|
|
|
|
" --target sys\t\tSet the target system\n"
|
2000-05-29 18:40:56 +00:00
|
|
|
|
" --verbose\t\tIncrease verbosity\n"
|
|
|
|
|
" --version\t\tPrint the assembler version\n",
|
2000-05-28 13:40:48 +00:00
|
|
|
|
ProgName);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static void SetOptions (void)
|
|
|
|
|
/* Set the option for the translator */
|
|
|
|
|
{
|
|
|
|
|
char Buf [256];
|
|
|
|
|
|
|
|
|
|
/* Set the translator */
|
|
|
|
|
sprintf (Buf, "ca65 V%u.%u.%u", VER_MAJOR, VER_MINOR, VER_PATCH);
|
|
|
|
|
OptTranslator (Buf);
|
|
|
|
|
|
|
|
|
|
/* Set date and time */
|
|
|
|
|
OptDateTime ((unsigned long) time(0));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static void DefineSymbol (const char* Def)
|
|
|
|
|
/* Define a symbol from the command line */
|
|
|
|
|
{
|
|
|
|
|
const char* P;
|
|
|
|
|
unsigned I;
|
|
|
|
|
long Val;
|
|
|
|
|
char SymName [MAX_STR_LEN+1];
|
2003-11-06 11:22:31 +00:00
|
|
|
|
SymEntry* Sym;
|
2003-11-28 22:12:14 +00:00
|
|
|
|
ExprNode* Expr;
|
2003-11-06 11:22:31 +00:00
|
|
|
|
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
|
|
/* The symbol must start with a character or underline */
|
2001-01-05 19:24:47 +00:00
|
|
|
|
if (Def [0] != '_' && !IsAlpha (Def [0])) {
|
2000-06-14 09:32:22 +00:00
|
|
|
|
InvDef (Def);
|
2000-05-28 13:40:48 +00:00
|
|
|
|
}
|
|
|
|
|
P = Def;
|
|
|
|
|
|
|
|
|
|
/* Copy the symbol, checking the rest */
|
|
|
|
|
I = 0;
|
2001-01-05 19:24:47 +00:00
|
|
|
|
while (IsAlNum (*P) || *P == '_') {
|
2000-05-28 13:40:48 +00:00
|
|
|
|
if (I <= MAX_STR_LEN) {
|
|
|
|
|
SymName [I++] = *P;
|
|
|
|
|
}
|
|
|
|
|
++P;
|
|
|
|
|
}
|
|
|
|
|
SymName [I] = '\0';
|
|
|
|
|
|
|
|
|
|
/* Do we have a value given? */
|
|
|
|
|
if (*P != '=') {
|
|
|
|
|
if (*P != '\0') {
|
2000-06-14 09:32:22 +00:00
|
|
|
|
InvDef (Def);
|
2000-05-28 13:40:48 +00:00
|
|
|
|
}
|
|
|
|
|
Val = 0;
|
|
|
|
|
} else {
|
|
|
|
|
/* We have a value */
|
|
|
|
|
++P;
|
|
|
|
|
if (*P == '$') {
|
|
|
|
|
++P;
|
|
|
|
|
if (sscanf (P, "%lx", &Val) != 1) {
|
2000-06-14 09:32:22 +00:00
|
|
|
|
InvDef (Def);
|
2000-05-28 13:40:48 +00:00
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
if (sscanf (P, "%li", &Val) != 1) {
|
2000-06-14 09:32:22 +00:00
|
|
|
|
InvDef (Def);
|
2000-05-28 13:40:48 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2003-11-06 11:22:31 +00:00
|
|
|
|
/* Search for the symbol, allocate a new one if it doesn't exist */
|
|
|
|
|
Sym = SymFind (CurrentScope, SymName, SYM_ALLOC_NEW);
|
|
|
|
|
|
2000-05-28 13:40:48 +00:00
|
|
|
|
/* Check if have already a symbol with this name */
|
2003-11-06 11:22:31 +00:00
|
|
|
|
if (SymIsDef (Sym)) {
|
2000-08-21 21:20:40 +00:00
|
|
|
|
AbEnd ("`%s' is already defined", SymName);
|
2000-05-28 13:40:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
2003-11-28 22:12:14 +00:00
|
|
|
|
/* Generate an expression for the symbol */
|
|
|
|
|
Expr = GenLiteralExpr (Val);
|
|
|
|
|
|
2003-11-06 11:22:31 +00:00
|
|
|
|
/* Mark the symbol as defined */
|
2003-11-28 22:12:14 +00:00
|
|
|
|
SymDef (Sym, Expr, ADDR_SIZE_DEFAULT, SF_NONE);
|
2000-05-28 13:40:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2001-09-30 22:19:39 +00:00
|
|
|
|
static void OptAutoImport (const char* Opt attribute ((unused)),
|
|
|
|
|
const char* Arg attribute ((unused)))
|
2000-05-29 18:40:56 +00:00
|
|
|
|
/* Mark unresolved symbols as imported */
|
|
|
|
|
{
|
|
|
|
|
AutoImport = 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2001-09-30 22:19:39 +00:00
|
|
|
|
static void OptCPU (const char* Opt attribute ((unused)), const char* Arg)
|
2000-05-28 13:40:48 +00:00
|
|
|
|
/* Handle the --cpu option */
|
|
|
|
|
{
|
2003-10-10 17:08:08 +00:00
|
|
|
|
cpu_t CPU = FindCPU (Arg);
|
|
|
|
|
if (CPU == CPU_UNKNOWN) {
|
2000-08-21 21:20:40 +00:00
|
|
|
|
AbEnd ("Invalid CPU: `%s'", Arg);
|
2003-10-10 17:08:08 +00:00
|
|
|
|
} else {
|
|
|
|
|
SetCPU (CPU);
|
2000-05-28 13:40:48 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2001-09-30 22:19:39 +00:00
|
|
|
|
static void OptDebugInfo (const char* Opt attribute ((unused)),
|
|
|
|
|
const char* Arg attribute ((unused)))
|
2000-05-29 18:40:56 +00:00
|
|
|
|
/* Add debug info to the object file */
|
|
|
|
|
{
|
|
|
|
|
DbgSyms = 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2001-09-30 22:19:39 +00:00
|
|
|
|
static void OptFeature (const char* Opt attribute ((unused)), const char* Arg)
|
2000-09-02 11:35:22 +00:00
|
|
|
|
/* Set an emulation feature */
|
|
|
|
|
{
|
|
|
|
|
/* Set the feature, check for errors */
|
|
|
|
|
if (SetFeature (Arg) == FEAT_UNKNOWN) {
|
|
|
|
|
AbEnd ("Illegal emulation feature: `%s'", Arg);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2001-09-30 22:19:39 +00:00
|
|
|
|
static void OptHelp (const char* Opt attribute ((unused)),
|
|
|
|
|
const char* Arg attribute ((unused)))
|
2000-06-14 07:22:25 +00:00
|
|
|
|
/* Print usage information and exit */
|
|
|
|
|
{
|
|
|
|
|
Usage ();
|
2000-06-14 09:32:22 +00:00
|
|
|
|
exit (EXIT_SUCCESS);
|
2000-06-14 07:22:25 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2001-09-30 22:19:39 +00:00
|
|
|
|
static void OptIgnoreCase (const char* Opt attribute ((unused)),
|
|
|
|
|
const char* Arg attribute ((unused)))
|
2000-05-29 18:40:56 +00:00
|
|
|
|
/* Ignore case on symbols */
|
|
|
|
|
{
|
|
|
|
|
IgnoreCase = 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2001-09-30 22:19:39 +00:00
|
|
|
|
static void OptIncludeDir (const char* Opt attribute ((unused)), const char* Arg)
|
2000-05-29 18:40:56 +00:00
|
|
|
|
/* Add an include search path */
|
|
|
|
|
{
|
|
|
|
|
AddIncludePath (Arg);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2001-09-30 22:19:39 +00:00
|
|
|
|
static void OptListing (const char* Opt attribute ((unused)),
|
|
|
|
|
const char* Arg attribute ((unused)))
|
2000-05-29 18:40:56 +00:00
|
|
|
|
/* Create a listing file */
|
|
|
|
|
{
|
|
|
|
|
Listing = 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2003-11-28 22:12:14 +00:00
|
|
|
|
static void OptMemoryModel (const char* Opt, const char* Arg)
|
2003-11-23 21:38:23 +00:00
|
|
|
|
/* Set the memory model */
|
|
|
|
|
{
|
2003-11-28 22:12:14 +00:00
|
|
|
|
mmodel_t M;
|
|
|
|
|
|
|
|
|
|
/* Check the current memory model */
|
|
|
|
|
if (MemoryModel != MMODEL_UNKNOWN) {
|
|
|
|
|
AbEnd ("Cannot use option `%s' twice", Opt);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Translate the memory model name and check it */
|
|
|
|
|
M = FindMemoryModel (Arg);
|
|
|
|
|
if (M == MMODEL_UNKNOWN) {
|
2003-11-23 21:38:23 +00:00
|
|
|
|
AbEnd ("Unknown memory model: %s", Arg);
|
2003-11-28 22:12:14 +00:00
|
|
|
|
} else if (M == MMODEL_HUGE) {
|
|
|
|
|
AbEnd ("Unsupported memory model: %s", Arg);
|
2003-11-23 21:38:23 +00:00
|
|
|
|
}
|
2003-11-28 22:12:14 +00:00
|
|
|
|
|
|
|
|
|
/* Set the memory model */
|
|
|
|
|
SetMemoryModel (M);
|
2003-11-23 21:38:23 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2001-09-30 22:19:39 +00:00
|
|
|
|
static void OptPageLength (const char* Opt attribute ((unused)), const char* Arg)
|
2000-05-28 13:40:48 +00:00
|
|
|
|
/* Handle the --pagelength option */
|
|
|
|
|
{
|
2001-09-30 22:19:39 +00:00
|
|
|
|
int Len = atoi (Arg);
|
2000-05-28 13:40:48 +00:00
|
|
|
|
if (Len != -1 && (Len < MIN_PAGE_LEN || Len > MAX_PAGE_LEN)) {
|
2000-08-21 21:20:40 +00:00
|
|
|
|
AbEnd ("Invalid page length: %d", Len);
|
2000-05-28 13:40:48 +00:00
|
|
|
|
}
|
|
|
|
|
PageLength = Len;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2001-09-30 22:19:39 +00:00
|
|
|
|
static void OptSmart (const char* Opt attribute ((unused)),
|
|
|
|
|
const char* Arg attribute ((unused)))
|
2000-05-28 13:40:48 +00:00
|
|
|
|
/* Handle the -s/--smart options */
|
|
|
|
|
{
|
|
|
|
|
SmartMode = 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2001-09-30 22:19:39 +00:00
|
|
|
|
static void OptTarget (const char* Opt attribute ((unused)), const char* Arg)
|
2000-08-21 21:20:40 +00:00
|
|
|
|
/* Set the target system */
|
|
|
|
|
{
|
|
|
|
|
/* Map the target name to a target id */
|
2000-08-23 07:01:18 +00:00
|
|
|
|
Target = FindTarget (Arg);
|
|
|
|
|
if (Target == TGT_UNKNOWN) {
|
2002-04-25 05:21:03 +00:00
|
|
|
|
AbEnd ("Invalid target name: `%s'", Arg);
|
|
|
|
|
} else if (Target == TGT_MODULE) {
|
|
|
|
|
AbEnd ("Cannot use `module' as a target for the assembler");
|
2000-08-21 21:20:40 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2001-09-30 22:19:39 +00:00
|
|
|
|
static void OptVerbose (const char* Opt attribute ((unused)),
|
|
|
|
|
const char* Arg attribute ((unused)))
|
2000-05-29 18:40:56 +00:00
|
|
|
|
/* Increase verbosity */
|
|
|
|
|
{
|
2001-03-10 10:21:03 +00:00
|
|
|
|
++Verbosity;
|
2000-05-29 18:40:56 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2001-09-30 22:19:39 +00:00
|
|
|
|
static void OptVersion (const char* Opt attribute ((unused)),
|
|
|
|
|
const char* Arg attribute ((unused)))
|
2000-05-29 18:40:56 +00:00
|
|
|
|
/* Print the assembler version */
|
|
|
|
|
{
|
|
|
|
|
fprintf (stderr,
|
2003-05-25 17:57:50 +00:00
|
|
|
|
"ca65 V%u.%u.%u - %s\n",
|
|
|
|
|
VER_MAJOR, VER_MINOR, VER_PATCH, Copyright);
|
2000-05-29 18:40:56 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2000-09-02 11:05:32 +00:00
|
|
|
|
static void DoPCAssign (void)
|
|
|
|
|
/* Start absolute code */
|
|
|
|
|
{
|
|
|
|
|
long PC = ConstExpression ();
|
|
|
|
|
if (PC < 0 || PC > 0xFFFFFF) {
|
2003-11-08 17:20:21 +00:00
|
|
|
|
Error ("Range error");
|
2000-09-02 11:05:32 +00:00
|
|
|
|
} else {
|
|
|
|
|
SetAbsPC (PC);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2000-05-28 13:40:48 +00:00
|
|
|
|
static void OneLine (void)
|
|
|
|
|
/* Assemble one line */
|
|
|
|
|
{
|
|
|
|
|
int Done = 0;
|
|
|
|
|
|
2000-07-09 13:19:25 +00:00
|
|
|
|
/* Initialize the new listing line if we are actually reading from file
|
2000-07-28 12:15:40 +00:00
|
|
|
|
* and not from internally pushed input.
|
2000-07-09 13:19:25 +00:00
|
|
|
|
*/
|
|
|
|
|
if (!HavePushedInput ()) {
|
2000-09-02 11:05:32 +00:00
|
|
|
|
InitListingLine ();
|
2000-08-01 21:36:45 +00:00
|
|
|
|
}
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
|
|
if (Tok == TOK_COLON) {
|
2000-09-02 11:05:32 +00:00
|
|
|
|
/* An unnamed label */
|
|
|
|
|
ULabDef ();
|
|
|
|
|
NextTok ();
|
2000-05-28 13:40:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Assemble the line */
|
|
|
|
|
if (Tok == TOK_IDENT) {
|
|
|
|
|
|
2000-09-02 11:05:32 +00:00
|
|
|
|
/* Is it a macro? */
|
|
|
|
|
if (IsMacro (SVal)) {
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
2000-09-02 11:05:32 +00:00
|
|
|
|
/* Yes, start a macro expansion */
|
|
|
|
|
MacExpandStart ();
|
|
|
|
|
Done = 1;
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
2000-09-02 11:05:32 +00:00
|
|
|
|
} else {
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
2000-09-02 11:05:32 +00:00
|
|
|
|
/* No, label. Remember the identifier, then skip it */
|
2003-11-06 11:22:31 +00:00
|
|
|
|
int HadWS = WS; /* Did we have whitespace before the ident? */
|
|
|
|
|
|
|
|
|
|
/* Generate the symbol table entry, then skip the name */
|
|
|
|
|
SymEntry* Sym = SymFind (CurrentScope, SVal, SYM_ALLOC_NEW);
|
2000-09-02 11:05:32 +00:00
|
|
|
|
NextTok ();
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
2000-09-02 11:05:32 +00:00
|
|
|
|
/* If a colon follows, this is a label definition. If there
|
|
|
|
|
* is no colon, it's an assignment.
|
|
|
|
|
*/
|
2003-10-17 00:38:21 +00:00
|
|
|
|
if (Tok == TOK_EQ || Tok == TOK_ASSIGN) {
|
|
|
|
|
/* If it's an assign token, we have a label */
|
2003-11-07 19:28:37 +00:00
|
|
|
|
unsigned Flags = (Tok == TOK_ASSIGN)? SF_LABEL : SF_NONE;
|
2000-09-02 11:05:32 +00:00
|
|
|
|
/* Skip the '=' */
|
|
|
|
|
NextTok ();
|
|
|
|
|
/* Define the symbol with the expression following the '=' */
|
2003-11-07 19:28:37 +00:00
|
|
|
|
SymDef (Sym, Expression(), ADDR_SIZE_DEFAULT, Flags);
|
2000-09-02 11:05:32 +00:00
|
|
|
|
/* Don't allow anything after a symbol definition */
|
|
|
|
|
Done = 1;
|
|
|
|
|
} else {
|
|
|
|
|
/* Define a label */
|
2003-11-07 19:28:37 +00:00
|
|
|
|
SymDef (Sym, GenCurrentPC (), ADDR_SIZE_DEFAULT, SF_LABEL);
|
2000-09-02 11:05:32 +00:00
|
|
|
|
/* Skip the colon. If NoColonLabels is enabled, allow labels
|
|
|
|
|
* without a colon if there is no whitespace before the
|
|
|
|
|
* identifier.
|
|
|
|
|
*/
|
2000-07-09 13:19:25 +00:00
|
|
|
|
if (Tok != TOK_COLON) {
|
|
|
|
|
if (HadWS || !NoColonLabels) {
|
2003-11-08 17:20:21 +00:00
|
|
|
|
Error ("`:' expected");
|
2000-07-09 13:19:25 +00:00
|
|
|
|
}
|
|
|
|
|
if (Tok == TOK_NAMESPACE) {
|
|
|
|
|
/* Smart :: handling */
|
|
|
|
|
NextTok ();
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
/* Skip the colon */
|
|
|
|
|
NextTok ();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2000-05-28 13:40:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!Done) {
|
|
|
|
|
|
2000-07-09 13:19:25 +00:00
|
|
|
|
if (TokIsPseudo (Tok)) {
|
|
|
|
|
/* A control command, IVal is index into table */
|
|
|
|
|
HandlePseudo ();
|
|
|
|
|
} else if (Tok == TOK_MNEMO) {
|
|
|
|
|
/* A mnemonic - assemble one instruction */
|
|
|
|
|
HandleInstruction (IVal);
|
|
|
|
|
} else if (Tok == TOK_IDENT && IsMacro (SVal)) {
|
|
|
|
|
/* A macro expansion */
|
|
|
|
|
MacExpandStart ();
|
2000-09-02 11:05:32 +00:00
|
|
|
|
} else if (PCAssignment && (Tok == TOK_STAR || Tok == TOK_PC)) {
|
|
|
|
|
NextTok ();
|
|
|
|
|
if (Tok != TOK_EQ) {
|
2003-11-08 17:20:21 +00:00
|
|
|
|
Error ("`=' expected");
|
2000-09-02 11:05:32 +00:00
|
|
|
|
SkipUntilSep ();
|
|
|
|
|
} else {
|
|
|
|
|
/* Skip the equal sign */
|
|
|
|
|
NextTok ();
|
|
|
|
|
/* Enter absolute mode */
|
|
|
|
|
DoPCAssign ();
|
|
|
|
|
}
|
|
|
|
|
}
|
2000-05-28 13:40:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Line separator must come here */
|
|
|
|
|
ConsumeSep ();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static void Assemble (void)
|
|
|
|
|
/* Start the ball rolling ... */
|
|
|
|
|
{
|
|
|
|
|
/* Prime the pump */
|
|
|
|
|
NextTok ();
|
|
|
|
|
|
|
|
|
|
/* Assemble lines until end of file */
|
|
|
|
|
while (Tok != TOK_EOF) {
|
|
|
|
|
OneLine ();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static void CreateObjFile (void)
|
|
|
|
|
/* Create the object file */
|
|
|
|
|
{
|
|
|
|
|
/* Open the object, write the header */
|
|
|
|
|
ObjOpen ();
|
|
|
|
|
|
|
|
|
|
/* Write the object file options */
|
|
|
|
|
WriteOptions ();
|
|
|
|
|
|
|
|
|
|
/* Write the list of input files */
|
|
|
|
|
WriteFiles ();
|
|
|
|
|
|
|
|
|
|
/* Write the segment data to the file */
|
|
|
|
|
WriteSegments ();
|
|
|
|
|
|
|
|
|
|
/* Write the import list */
|
|
|
|
|
WriteImports ();
|
|
|
|
|
|
|
|
|
|
/* Write the export list */
|
|
|
|
|
WriteExports ();
|
|
|
|
|
|
|
|
|
|
/* Write debug symbols if requested */
|
|
|
|
|
WriteDbgSyms ();
|
|
|
|
|
|
2001-05-23 19:03:40 +00:00
|
|
|
|
/* Write line infos if requested */
|
|
|
|
|
WriteLineInfo ();
|
|
|
|
|
|
2003-05-25 17:57:50 +00:00
|
|
|
|
/* Write the string pool */
|
|
|
|
|
WriteStrPool ();
|
|
|
|
|
|
2003-06-06 12:45:19 +00:00
|
|
|
|
/* Write the assertions */
|
|
|
|
|
WriteAssertions ();
|
|
|
|
|
|
2000-05-28 13:40:48 +00:00
|
|
|
|
/* Write an updated header and close the file */
|
|
|
|
|
ObjClose ();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int main (int argc, char* argv [])
|
|
|
|
|
/* Assembler main program */
|
|
|
|
|
{
|
2000-06-14 07:22:25 +00:00
|
|
|
|
/* Program long options */
|
|
|
|
|
static const LongOpt OptTab[] = {
|
|
|
|
|
{ "--auto-import", 0, OptAutoImport },
|
|
|
|
|
{ "--cpu", 1, OptCPU },
|
|
|
|
|
{ "--debug-info", 0, OptDebugInfo },
|
2000-09-02 11:35:22 +00:00
|
|
|
|
{ "--feature", 1, OptFeature },
|
|
|
|
|
{ "--help", 0, OptHelp },
|
2000-06-14 07:22:25 +00:00
|
|
|
|
{ "--ignore-case", 0, OptIgnoreCase },
|
|
|
|
|
{ "--include-dir", 1, OptIncludeDir },
|
2000-09-02 11:35:22 +00:00
|
|
|
|
{ "--listing", 0, OptListing },
|
2003-11-23 21:38:23 +00:00
|
|
|
|
{ "--memory-model", 1, OptMemoryModel },
|
2000-06-14 07:22:25 +00:00
|
|
|
|
{ "--pagelength", 1, OptPageLength },
|
2000-09-02 11:35:22 +00:00
|
|
|
|
{ "--smart", 0, OptSmart },
|
|
|
|
|
{ "--target", 1, OptTarget },
|
|
|
|
|
{ "--verbose", 0, OptVerbose },
|
|
|
|
|
{ "--version", 0, OptVersion },
|
2000-06-14 07:22:25 +00:00
|
|
|
|
};
|
|
|
|
|
|
2001-09-15 11:51:08 +00:00
|
|
|
|
unsigned I;
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
2000-06-14 07:22:25 +00:00
|
|
|
|
/* Initialize the cmdline module */
|
2001-03-09 21:59:23 +00:00
|
|
|
|
InitCmdLine (&argc, &argv, "ca65");
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
|
|
/* Enter the base lexical level. We must do that here, since we may
|
|
|
|
|
* define symbols using -D.
|
|
|
|
|
*/
|
2003-11-13 00:21:31 +00:00
|
|
|
|
SymEnterLevel ("", ST_GLOBAL, ADDR_SIZE_DEFAULT);
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
|
|
/* Check the parameters */
|
|
|
|
|
I = 1;
|
2001-03-09 21:59:23 +00:00
|
|
|
|
while (I < ArgCount) {
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
|
|
/* Get the argument */
|
2001-03-09 21:59:23 +00:00
|
|
|
|
const char* Arg = ArgVec [I];
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
|
|
/* Check for an option */
|
2003-11-23 21:38:23 +00:00
|
|
|
|
if (Arg[0] == '-') {
|
|
|
|
|
switch (Arg[1]) {
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
2003-11-23 21:38:23 +00:00
|
|
|
|
case '-':
|
|
|
|
|
LongOption (&I, OptTab, sizeof(OptTab)/sizeof(OptTab[0]));
|
|
|
|
|
break;
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
2003-11-23 21:38:23 +00:00
|
|
|
|
case 'g':
|
|
|
|
|
OptDebugInfo (Arg, 0);
|
|
|
|
|
break;
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
2003-11-23 21:38:23 +00:00
|
|
|
|
case 'h':
|
|
|
|
|
OptHelp (Arg, 0);
|
|
|
|
|
break;
|
2000-07-08 21:01:02 +00:00
|
|
|
|
|
2000-05-28 13:40:48 +00:00
|
|
|
|
case 'i':
|
2003-11-23 21:38:23 +00:00
|
|
|
|
OptIgnoreCase (Arg, 0);
|
|
|
|
|
break;
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
2003-11-23 21:38:23 +00:00
|
|
|
|
case 'l':
|
|
|
|
|
OptListing (Arg, 0);
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case 'm':
|
|
|
|
|
if (Arg[2] == 'm') {
|
|
|
|
|
OptMemoryModel (Arg, GetArg (&I, 3));
|
|
|
|
|
} else {
|
|
|
|
|
UnknownOption (Arg);
|
|
|
|
|
}
|
|
|
|
|
break;
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
|
|
case 'o':
|
2000-06-14 07:22:25 +00:00
|
|
|
|
OutFile = GetArg (&I, 2);
|
2001-09-15 11:51:08 +00:00
|
|
|
|
break;
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
|
|
case 's':
|
2000-06-14 07:22:25 +00:00
|
|
|
|
OptSmart (Arg, 0);
|
2000-05-28 13:40:48 +00:00
|
|
|
|
break;
|
|
|
|
|
|
2000-08-21 21:20:40 +00:00
|
|
|
|
case 't':
|
|
|
|
|
OptTarget (Arg, GetArg (&I, 2));
|
|
|
|
|
break;
|
|
|
|
|
|
2000-05-28 13:40:48 +00:00
|
|
|
|
case 'v':
|
2000-06-14 07:22:25 +00:00
|
|
|
|
OptVerbose (Arg, 0);
|
2000-05-28 13:40:48 +00:00
|
|
|
|
break;
|
|
|
|
|
|
2000-05-29 18:40:56 +00:00
|
|
|
|
case 'D':
|
2000-06-14 07:22:25 +00:00
|
|
|
|
DefineSymbol (GetArg (&I, 2));
|
2000-05-29 18:40:56 +00:00
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case 'I':
|
2000-06-14 07:22:25 +00:00
|
|
|
|
OptIncludeDir (Arg, GetArg (&I, 2));
|
2000-05-29 18:40:56 +00:00
|
|
|
|
break;
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
|
|
case 'U':
|
2000-06-14 07:22:25 +00:00
|
|
|
|
OptAutoImport (Arg, 0);
|
2000-05-28 13:40:48 +00:00
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case 'V':
|
2000-09-14 19:26:13 +00:00
|
|
|
|
OptVersion (Arg, 0);
|
2000-05-28 13:40:48 +00:00
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case 'W':
|
2000-06-14 07:22:25 +00:00
|
|
|
|
WarnLevel = atoi (GetArg (&I, 2));
|
2000-05-28 13:40:48 +00:00
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
UnknownOption (Arg);
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
/* Filename. Check if we already had one */
|
|
|
|
|
if (InFile) {
|
2000-06-14 09:32:22 +00:00
|
|
|
|
fprintf (stderr, "%s: Don't know what to do with `%s'\n",
|
2000-08-23 07:01:18 +00:00
|
|
|
|
ProgName, Arg);
|
|
|
|
|
exit (EXIT_FAILURE);
|
2000-05-28 13:40:48 +00:00
|
|
|
|
} else {
|
2000-08-23 07:01:18 +00:00
|
|
|
|
InFile = Arg;
|
2000-05-28 13:40:48 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Next argument */
|
|
|
|
|
++I;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Do we have an input file? */
|
|
|
|
|
if (InFile == 0) {
|
2000-06-14 09:32:22 +00:00
|
|
|
|
fprintf (stderr, "%s: No input files\n", ProgName);
|
2000-05-28 13:40:48 +00:00
|
|
|
|
exit (EXIT_FAILURE);
|
|
|
|
|
}
|
|
|
|
|
|
2003-05-01 23:24:20 +00:00
|
|
|
|
/* If no CPU given, use the default CPU for the target */
|
2003-05-25 17:57:50 +00:00
|
|
|
|
if (GetCPU () == CPU_UNKNOWN) {
|
2003-05-01 23:24:20 +00:00
|
|
|
|
if (Target != TGT_UNKNOWN) {
|
|
|
|
|
SetCPU (DefaultCPU[Target]);
|
|
|
|
|
} else {
|
|
|
|
|
SetCPU (CPU_6502);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2003-11-28 22:12:14 +00:00
|
|
|
|
/* If no memory model was given, use the default */
|
|
|
|
|
if (MemoryModel == MMODEL_UNKNOWN) {
|
|
|
|
|
MemoryModel = MMODEL_NEAR;
|
|
|
|
|
}
|
|
|
|
|
|
2000-09-14 19:26:13 +00:00
|
|
|
|
/* Intialize the target translation tables */
|
|
|
|
|
TgtTranslateInit ();
|
|
|
|
|
|
2003-11-28 22:12:14 +00:00
|
|
|
|
/* Initialize the segments */
|
|
|
|
|
InitSegments ();
|
|
|
|
|
|
2000-05-28 13:40:48 +00:00
|
|
|
|
/* Initialize the scanner, open the input file */
|
|
|
|
|
InitScanner (InFile);
|
|
|
|
|
|
|
|
|
|
/* Define the default options */
|
|
|
|
|
SetOptions ();
|
|
|
|
|
|
|
|
|
|
/* Assemble the input */
|
|
|
|
|
Assemble ();
|
|
|
|
|
|
2002-11-28 17:42:16 +00:00
|
|
|
|
/* If we didn't have any errors, check the segment stack */
|
|
|
|
|
if (ErrorCount == 0) {
|
|
|
|
|
SegStackCheck ();
|
|
|
|
|
}
|
|
|
|
|
|
2000-05-28 13:40:48 +00:00
|
|
|
|
/* If we didn't have any errors, check the unnamed labels */
|
|
|
|
|
if (ErrorCount == 0) {
|
2000-05-29 18:40:56 +00:00
|
|
|
|
ULabCheck ();
|
2000-05-28 13:40:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* If we didn't have any errors, check the symbol table */
|
|
|
|
|
if (ErrorCount == 0) {
|
|
|
|
|
SymCheck ();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* If we didn't have any errors, check and resolve the segment data */
|
|
|
|
|
if (ErrorCount == 0) {
|
|
|
|
|
SegCheck ();
|
|
|
|
|
}
|
|
|
|
|
|
2001-05-23 19:03:40 +00:00
|
|
|
|
/* If we didn't have an errors, index the line infos */
|
|
|
|
|
MakeLineInfoIndex ();
|
|
|
|
|
|
2000-05-28 13:40:48 +00:00
|
|
|
|
/* Dump the data */
|
2001-03-10 10:21:03 +00:00
|
|
|
|
if (Verbosity >= 2) {
|
2000-05-28 13:40:48 +00:00
|
|
|
|
SymDump (stdout);
|
|
|
|
|
SegDump ();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* If we didn't have any errors, create the object and listing files */
|
|
|
|
|
if (ErrorCount == 0) {
|
|
|
|
|
CreateObjFile ();
|
|
|
|
|
if (Listing) {
|
|
|
|
|
CreateListing ();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Close the input file */
|
|
|
|
|
DoneScanner ();
|
|
|
|
|
|
|
|
|
|
/* Return an apropriate exit code */
|
|
|
|
|
return (ErrorCount == 0)? EXIT_SUCCESS : EXIT_FAILURE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|