vc2022重新编的 cc65 编译器
立即下载
资源介绍:
cc65 可用于apple ii/e,任天堂红白机,6502,65c02,65186,65c186单片机。版本CC65 2.9.2
/*****************************************************************************/
/* */
/* codegen.c */
/* */
/* 6502 code generator */
/* */
/* */
/* */
/* (C) 1998-2002 Ullrich von Bassewitz */
/* Wacholderweg 14 */
/* D-70597 Stuttgart */
/* EMail: uz@cc65.org */
/* */
/* */
/* 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
#include
#include
/* common */
#include "check.h"
#include "strbuf.h"
#include "version.h"
#include "xmalloc.h"
#include "xsprintf.h"
/* cc65 */
#include "asmcode.h"
#include "asmlabel.h"
#include "casenode.h"
#include "codeseg.h"
#include "cpu.h"
#include "dataseg.h"
#include "error.h"
#include "global.h"
#include "segments.h"
#include "textseg.h"
#include "util.h"
#include "codegen.h"
/*****************************************************************************/
/* Data */
/*****************************************************************************/
/* Compiler relative stack pointer */
int oursp = 0;
/*****************************************************************************/
/* Helpers */
/*****************************************************************************/
static void typeerror (unsigned type)
/* Print an error message about an invalid operand type */
{
Internal ("Invalid type in CF flags: %04X, type = %u", type, type & CF_TYPE);
}
static void CheckLocalOffs (unsigned Offs)
/* Check the offset into the stack for 8bit range */
{
if (Offs >= 256) {
/* Too many local vars */
Error ("Too many local variables");
}
}
static const char* GetLabelName (unsigned Flags, unsigned long Label, long Offs)
{
static char Buf [256]; /* Label name */
/* Create the correct label name */
switch (Flags & CF_ADDRMASK) {
case CF_STATIC:
/* Static memory cell */
if (Offs) {
xsprintf (Buf, sizeof (Buf), "%s%+ld", LocalLabelName (Label), Offs);
} else {
xsprintf (Buf, sizeof (Buf), "%s", LocalLabelName (Label));
}
break;
case CF_EXTERNAL:
/* External label */
if (Offs) {
xsprintf (Buf, sizeof (Buf), "_%s%+ld", (char*) Label, Offs);
} else {
xsprintf (Buf, sizeof (Buf), "_%s", (char*) Label);
}
break;
case CF_ABSOLUTE:
/* Absolute address */
xsprintf (Buf, sizeof (Buf), "$%04X", (int)((Label+Offs) & 0xFFFF));
break;
case CF_REGVAR:
/* Variable in register bank */
xsprintf (Buf, sizeof (Buf), "regbank+%u", (unsigned)((Label+Offs) & 0xFFFF));
break;
default:
Internal ("Invalid address flags: %04X", Flags);
}
/* Return a pointer to the static buffer */
return Buf;
}
/*****************************************************************************/
/* Pre- and postamble */
/*****************************************************************************/
void g_preamble (void)
/* Generate the assembler code preamble */
{
/* Create a new (global) segment list and remember it */
PushSegments (0);
GS = CS;
/* Identify the compiler version */
AddTextLine (";");
AddTextLine ("; File generated by cc65 v %u.%u.%u",
VER_MAJOR, VER_MINOR, VER_PATCH);
AddTextLine (";");
/* Insert some object file options */
AddTextLine ("\t.fopt\t\tcompiler,\"cc65 v %u.%u.%u\"",
VER_MAJOR, VER_MINOR, VER_PATCH);
/* If we're producing code for some other CPU, switch the command set */
if (CPU == CPU_65C02) {
AddTextLine ("\t.pc02");
}
/* Allow auto import for runtime library routines */
AddTextLine ("\t.autoimport\ton");
/* Switch the assembler into case sensitive mode */
AddTextLine ("\t.case\t\ton");
/* Tell the assembler if we want to generate debug info */
AddTextLine ("\t.debuginfo\t%s", (DebugInfo != 0)? "on" : "off");
/* Import the stack pointer for direct auto variable access */
AddTextLine ("\t.importzp\tsp, sreg, regsave, regbank, tmp1, ptr1, ptr2");
/* Define long branch macros */
AddTextLine ("\t.macpack\tlongbranch");
}
void g_fileinfo (const char* Name, unsigned long Size, unsigned long MTime)
/* If debug info is enabled, place a file info into the source */
{
if (DebugInfo) {
/* We have to place this into the global text segment, so it will
* appear before all .dbg line statements.
*/
TS_AddLine (GS->Text, "\t.dbg\t\tfile, \"%s\", %lu, %lu", Name, Size, MTime);
}
}
/*****************************************************************************/
/* Segment support */
/*****************************************************************************/
void g_userodata (void)
/* Switch to the read only data segment */
{
UseDataSeg (SEG_RODATA);
}
void g_usedata (void)
/* Switch to the data segment */
{
UseDataSeg (SEG_DATA);
}
void g_usebss (void)
/* Switch to the bss segment */
{
UseDataSeg (SEG_BSS);
}
void g_segname (segment_t Seg, const char* Name)
/* Set the name of a segment */
{
DataSeg* S;
/* Remember the new name */
NewSegName (Seg, Name);
/* Emit a segment directive for the data style segments */
switch (Seg) {
case SEG_RODATA: S = CS->ROData; break;
case SEG_DATA: S = CS->Data; break;
case SEG_BSS: S = CS->BSS; break;
default: S = 0; break;
}
if (S) {
DS_AddLine (S, ".segment\t\"%s\"", Name);
}
}
/*****************************************************************************/
/* Code */
/*****************************************************************************/
unsigned sizeofarg (unsigned flags)
/* Return the size of a function argument type that is encoded in flags */
{
switch (flags & CF_TYPE) {
case CF_CHAR:
return (flags & CF_FORC
资源文件列表:
cc65.zip 大约有734个文件