Merge pull request #2760 from kugelfuhr/kugelfuhr/fix-2753

Add ".CAPABILITY" to ca65, remove ".MACPACK cpu"
This commit is contained in:
Bob Andrews
2025-07-03 18:43:05 +02:00
committed by GitHub
84 changed files with 717 additions and 361 deletions

View File

@@ -37,6 +37,7 @@
#include <time.h>
/* common */
#include "capability.h"
#include "check.h"
#include "cpu.h"
#include "exprdefs.h"
@@ -60,6 +61,7 @@
#include "studyexpr.h"
#include "symbol.h"
#include "symtab.h"
#include "target.h"
#include "toklist.h"
#include "ulabel.h"
#include "macro.h"
@@ -405,6 +407,66 @@ static ExprNode* FuncBlank (void)
static ExprNode* FuncCapability (void)
/* Handle the .CAPABILITY builtin function */
{
int Result = 1;
/* What follows is a comma separated list of identifiers. An empty list is
** not allowed.
*/
while (1) {
const char* Name;
capability_t Cap;
/* We must have an identifier */
if (CurTok.Tok != TOK_IDENT) {
Error ("Arguments to .CAPABILITY must be identifiers");
/* Skip tokens until closing paren or end of line */
while (CurTok.Tok != TOK_RPAREN && !TokIsSep (CurTok.Tok)) {
NextTok ();
}
return GenLiteral0 ();
}
/* Search for the capability that matches this identifier. Ignore case
** on the specified capabilities.
*/
UpcaseSVal ();
SB_Terminate (&CurTok.SVal);
Name = SB_GetConstBuf (&CurTok.SVal);
Cap = FindCapability (Name);
/* Check if the capability is supported */
if (Cap == CAP_INVALID) {
Error ("Not a valid capability name: %s", Name);
Result = 0;
} else {
/* The pseudo function result is the logical AND of all capabilities
** given.
*/
if (!CPUHasCap (Cap) && !TargetHasCap (Cap)) {
Result = 0;
}
}
/* Skip the capability name */
NextTok ();
/* Handle end of list or next capability */
if (CurTok.Tok != TOK_COMMA) {
break;
}
NextTok ();
}
/* Done */
return GenLiteralExpr (Result);
}
static ExprNode* FuncConst (void)
/* Handle the .CONST builtin function */
{
@@ -484,9 +546,10 @@ static ExprNode* FuncIsMnemonic (void)
if (FindMacro (&CurTok.SVal) == 0) {
Instr = FindInstruction (&CurTok.SVal);
}
}
else {
/* Macros and symbols may NOT use the names of instructions, so just check for the instruction */
} else {
/* Macros and symbols may NOT use the names of instructions, so
** just check for the instruction.
*/
Instr = FindInstruction (&CurTok.SVal);
}
}
@@ -532,7 +595,7 @@ static ExprNode* DoMatch (enum TC EqualityLevel)
token_t Term = GetTokListTerm (TOK_COMMA);
while (CurTok.Tok != Term) {
/* We may not end-of-line of end-of-file here */
/* We may not end-of-line or end-of-file here */
if (TokIsSep (CurTok.Tok)) {
Error ("Unexpected end of line");
return GenLiteral0 ();
@@ -570,7 +633,7 @@ static ExprNode* DoMatch (enum TC EqualityLevel)
Node = Root;
while (CurTok.Tok != Term) {
/* We may not end-of-line of end-of-file here */
/* We may not end-of-line or end-of-file here */
if (TokIsSep (CurTok.Tok)) {
Error ("Unexpected end of line");
return GenLiteral0 ();
@@ -1129,6 +1192,10 @@ static ExprNode* Factor (void)
N = Function (FuncBlank);
break;
case TOK_CAP:
N = Function (FuncCapability);
break;
case TOK_CONST:
N = Function (FuncConst);
break;

View File

@@ -192,6 +192,45 @@ static void CBMSystem (const char* Sys)
static void DefineCpuSymbols (void)
/* Define all the symbols to evaluate .cpu. These were previously in cpu.mac. */
{
NewSymbol ("CPU_ISET_NONE", CPU_ISET_NONE);
NewSymbol ("CPU_ISET_6502", CPU_ISET_6502);
NewSymbol ("CPU_ISET_6502X", CPU_ISET_6502X);
NewSymbol ("CPU_ISET_6502DTV", CPU_ISET_6502DTV);
NewSymbol ("CPU_ISET_65SC02", CPU_ISET_65SC02);
NewSymbol ("CPU_ISET_65C02", CPU_ISET_65C02);
NewSymbol ("CPU_ISET_65816", CPU_ISET_65816);
NewSymbol ("CPU_ISET_SWEET16", CPU_ISET_SWEET16);
NewSymbol ("CPU_ISET_HUC6280", CPU_ISET_HUC6280);
NewSymbol ("CPU_ISET_M740", CPU_ISET_M740);
NewSymbol ("CPU_ISET_4510", CPU_ISET_4510);
NewSymbol ("CPU_ISET_45GS02", CPU_ISET_45GS02);
NewSymbol ("CPU_ISET_W65C02", CPU_ISET_W65C02);
NewSymbol ("CPU_ISET_65CE02", CPU_ISET_65CE02);
/* Additional ones from cpu.mac. Not sure how useful they are after the
** changes from #2751.
*/
NewSymbol ("CPU_NONE", CPUIsets[CPU_NONE]);
NewSymbol ("CPU_6502", CPUIsets[CPU_6502]);
NewSymbol ("CPU_6502X", CPUIsets[CPU_6502X]);
NewSymbol ("CPU_6502DTV", CPUIsets[CPU_6502DTV]);
NewSymbol ("CPU_65SC02", CPUIsets[CPU_65SC02]);
NewSymbol ("CPU_65C02", CPUIsets[CPU_65C02]);
NewSymbol ("CPU_65816", CPUIsets[CPU_65816]);
NewSymbol ("CPU_SWEET16", CPUIsets[CPU_SWEET16]);
NewSymbol ("CPU_HUC6280", CPUIsets[CPU_HUC6280]);
NewSymbol ("CPU_M740", CPUIsets[CPU_M740]);
NewSymbol ("CPU_4510", CPUIsets[CPU_4510]);
NewSymbol ("CPU_45GS02", CPUIsets[CPU_45GS02]);
NewSymbol ("CPU_W65C02", CPUIsets[CPU_W65C02]);
NewSymbol ("CPU_65CE02", CPUIsets[CPU_65CE02]);
}
static void SetSys (const char* Sys)
/* Define a target system */
{
@@ -363,6 +402,9 @@ static void SetSys (const char* Sys)
}
/* Define the symbols for evaluating .cpu */
DefineCpuSymbols ();
/* Initialize the translation tables for the target system */
TgtTranslateInit ();
}

View File

@@ -2114,7 +2114,8 @@ struct CtrlDesc {
};
/* NOTE: .AND, .BITAND, .BITNOT, .BITOR, .BITXOR, .MOD, .NOT, .OR, .SHL, .SHR
and .XOR do NOT go into this table */
** and .XOR do NOT go into this table.
*/
#define PSEUDO_COUNT (sizeof (CtrlCmdTab) / sizeof (CtrlCmdTab [0]))
static CtrlDesc CtrlCmdTab [] = {
{ ccNone, DoA16 }, /* .A16 */
@@ -2132,6 +2133,7 @@ static CtrlDesc CtrlCmdTab [] = {
{ ccNone, DoUnexpected }, /* .BLANK */
{ ccNone, DoBss }, /* .BSS */
{ ccNone, DoByte }, /* .BYT, .BYTE */
{ ccNone, DoUnexpected }, /* .CAP */
{ ccNone, DoCase }, /* .CASE */
{ ccNone, DoCharMap }, /* .CHARMAP */
{ ccNone, DoCode }, /* .CODE */

View File

@@ -158,6 +158,8 @@ struct DotKeyword {
{ ".BSS", TOK_BSS },
{ ".BYT", TOK_BYTE },
{ ".BYTE", TOK_BYTE },
{ ".CAP", TOK_CAP },
{ ".CAPABILITY", TOK_CAP },
{ ".CASE", TOK_CASE },
{ ".CHARMAP", TOK_CHARMAP },
{ ".CODE", TOK_CODE },

View File

@@ -137,6 +137,7 @@ typedef enum token_t {
TOK_BLANK,
TOK_BSS,
TOK_BYTE,
TOK_CAP,
TOK_CASE,
TOK_CHARMAP,
TOK_CODE,

View File

@@ -80,6 +80,7 @@
<ClInclude Include="common\assertion.h" />
<ClInclude Include="common\attrib.h" />
<ClInclude Include="common\bitops.h" />
<ClInclude Include="common\capability.h" />
<ClInclude Include="common\cddefs.h" />
<ClInclude Include="common\chartype.h" />
<ClInclude Include="common\check.h" />
@@ -133,6 +134,7 @@
<ClCompile Include="common\alignment.c" />
<ClCompile Include="common\assertion.c" />
<ClCompile Include="common\bitops.c" />
<ClCompile Include="common\capability.c" />
<ClCompile Include="common\chartype.c" />
<ClCompile Include="common\check.c" />
<ClCompile Include="common\cmdline.c" />
@@ -171,4 +173,4 @@
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>
</Project>

91
src/common/capability.c Normal file
View File

@@ -0,0 +1,91 @@
/*****************************************************************************/
/* */
/* capability.c */
/* */
/* Handle CPU or target capabilities */
/* */
/* */
/* */
/* (C) 2026, Kugelfuhr */
/* */
/* */
/* 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 <stdlib.h>
/* ca65 */
#include "capability.h"
/*****************************************************************************/
/* Data */
/*****************************************************************************/
/* List of dot keywords with the corresponding ids. */
/* CAUTION: table must be sorted for bsearch. */
struct Capability {
const char* Key;
capability_t Cap;
} Capabilities [] = {
/* BEGIN SORTED.SH */
{ "CPU_HAS_BITIMM", CAP_CPU_HAS_BITIMM },
{ "CPU_HAS_BRA8", CAP_CPU_HAS_BRA8 },
{ "CPU_HAS_INA", CAP_CPU_HAS_INA },
{ "CPU_HAS_PUSHXY", CAP_CPU_HAS_PUSHXY },
{ "CPU_HAS_STZ", CAP_CPU_HAS_STZ },
{ "CPU_HAS_ZPIND", CAP_CPU_HAS_ZPIND },
/* END SORTED.SH */
};
#define CAP_TABLE_SIZE (sizeof (Capabilities) / sizeof (Capabilities [0]))
/*****************************************************************************/
/* Code */
/*****************************************************************************/
static int CmpCapability (const void* K1, const void* K2)
/* Compare function for the capability search */
{
return strcmp (((struct Capability*)K1)->Key, ((struct Capability*)K2)->Key);
}
capability_t FindCapability (const char* Name)
/* Find the capability with the given name. Returns CAP_INVALID if there is no
** capability with the given name and a capability code >= 0 instead. The
** capability name is expected in upper case.
*/
{
const struct Capability K = { Name, 0 };
const struct Capability* C = bsearch (&K, Capabilities, CAP_TABLE_SIZE,
sizeof (Capabilities [0]),
CmpCapability);
return (C == 0)? CAP_INVALID : C->Cap;
}

79
src/common/capability.h Normal file
View File

@@ -0,0 +1,79 @@
/*****************************************************************************/
/* */
/* capability.h */
/* */
/* Handle CPU or target capabilities */
/* */
/* */
/* */
/* (C) 2026, Kugelfuhr */
/* */
/* */
/* 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. */
/* */
/*****************************************************************************/
#ifndef CAPABILITY_H
#define CAPABILITY_H
/* common */
#include "strbuf.h"
/*****************************************************************************/
/* Data */
/*****************************************************************************/
/* Numeric codes for capabilities */
enum capability_t {
CAP_INVALID = -1,
CAP_CPU_HAS_BRA8 = 0, /* CPU has a BRA 8-bit instruction */
CAP_CPU_HAS_INA = 1, /* CPU has DEA/INA */
CAP_CPU_HAS_PUSHXY = 2, /* CPU has PHX/PHY/PLX/PLY */
CAP_CPU_HAS_ZPIND = 3, /* CPU has "(zp)" mode (no offset) */
CAP_CPU_HAS_STZ = 4, /* CPU has "store zero" (!) instruction */
CAP_CPU_HAS_BITIMM = 5, /* CPU has "bit #imm" instruction */
};
typedef enum capability_t capability_t;
/*****************************************************************************/
/* Code */
/*****************************************************************************/
capability_t FindCapability (const char* Name);
/* Find the capability with the given name. Returns CAP_INVALID if there is no
** capability with the given name and a capability code >= 0 instead. The
** capability name is expected in upper case.
*/
/* End of capability.h */
#endif

View File

@@ -33,6 +33,8 @@
#include <stdint.h>
/* common */
#include "addrsize.h"
#include "check.h"
@@ -72,24 +74,112 @@ const char* CPUNames[CPU_COUNT] = {
* NOTE: make sure to only combine the instruction sets that are 100% compatible
*/
const unsigned CPUIsets[CPU_COUNT] = {
/* CPU_NONE */
CPU_ISET_NONE,
/* CPU_6502 */
CPU_ISET_6502,
/* CPU_6502X */
CPU_ISET_6502X | CPU_ISET_6502,
/* CPU_6502DTV */
CPU_ISET_6502DTV | CPU_ISET_6502,
/* CPU_65SC02 */
CPU_ISET_65SC02 | CPU_ISET_6502,
/* CPU_65C02 */
CPU_ISET_65C02 | CPU_ISET_6502 | CPU_ISET_65SC02,
/* 65816 has wai/stp and NO bit manipulation */
/* CPU_65816. 65816 has wai/stp and NO bit manipulation. */
CPU_ISET_65816 | CPU_ISET_6502 | CPU_ISET_65SC02,
/* CPU_SWEET16 */
CPU_ISET_SWEET16,
/* CPU_HUC6280 */
CPU_ISET_HUC6280 | CPU_ISET_6502 | CPU_ISET_65SC02 | CPU_ISET_65C02,
/* CPU_M740 */
CPU_ISET_M740 | CPU_ISET_6502,
/* 4510 does NOT have indirect-zp (without z), so we can not use 65SC02 */
/* CPU_4510. 4510 does NOT have indirect-zp (without z), so we can not use 65SC02 */
CPU_ISET_4510 | CPU_ISET_6502 | CPU_ISET_65C02 | CPU_ISET_65CE02,
/* CPU_45GS02 */
CPU_ISET_45GS02 | CPU_ISET_6502 | CPU_ISET_65C02 | CPU_ISET_65CE02 | CPU_ISET_4510,
/* CPU_W65C02 */
CPU_ISET_W65C02 | CPU_ISET_6502 | CPU_ISET_65SC02 | CPU_ISET_65C02,
/* CPU_65CE02 */
CPU_ISET_65CE02 | CPU_ISET_6502 | CPU_ISET_65C02,
};
/* Defines for capabilities. Currently the entries are uint32_ts but the table
** is deliberately hidden from the outside so it can be extended to 64 bit or
** even more.
*/
#define CAP_BIT(Cap) (UINT32_C (1) << (Cap))
#define CAP_NONE UINT32_C (0)
#define CAP_6502 CAP_NONE
#define CAP_6502X CAP_NONE
#define CAP_6502DTV CAP_NONE
#define CAP_65SC02 \
(CAP_BIT (CAP_CPU_HAS_BITIMM) | \
CAP_BIT (CAP_CPU_HAS_BRA8) | \
CAP_BIT (CAP_CPU_HAS_INA) | \
CAP_BIT (CAP_CPU_HAS_PUSHXY) | \
CAP_BIT (CAP_CPU_HAS_ZPIND) | \
CAP_BIT (CAP_CPU_HAS_STZ))
#define CAP_65C02 \
(CAP_BIT (CAP_CPU_HAS_BITIMM) | \
CAP_BIT (CAP_CPU_HAS_BRA8) | \
CAP_BIT (CAP_CPU_HAS_INA) | \
CAP_BIT (CAP_CPU_HAS_PUSHXY) | \
CAP_BIT (CAP_CPU_HAS_ZPIND) | \
CAP_BIT (CAP_CPU_HAS_STZ))
#define CAP_65816 \
(CAP_BIT (CAP_CPU_HAS_BITIMM) | \
CAP_BIT (CAP_CPU_HAS_BRA8) | \
CAP_BIT (CAP_CPU_HAS_INA) | \
CAP_BIT (CAP_CPU_HAS_PUSHXY) | \
CAP_BIT (CAP_CPU_HAS_ZPIND) | \
CAP_BIT (CAP_CPU_HAS_STZ))
#define CAP_SWEET16 CAP_NONE
#define CAP_HUC6280 \
(CAP_BIT (CAP_CPU_HAS_BITIMM) | \
CAP_BIT (CAP_CPU_HAS_BRA8) | \
CAP_BIT (CAP_CPU_HAS_INA) | \
CAP_BIT (CAP_CPU_HAS_PUSHXY) | \
CAP_BIT (CAP_CPU_HAS_ZPIND) | \
CAP_BIT (CAP_CPU_HAS_STZ))
#define CAP_M740 \
(CAP_BIT (CAP_CPU_HAS_BRA8) | \
CAP_BIT (CAP_CPU_HAS_INA))
#define CAP_4510 \
(CAP_BIT (CAP_CPU_HAS_BRA8) | \
CAP_BIT (CAP_CPU_HAS_INA) | \
CAP_BIT (CAP_CPU_HAS_PUSHXY))
#define CAP_45GS02 \
(CAP_BIT (CAP_CPU_HAS_BRA8) | \
CAP_BIT (CAP_CPU_HAS_INA) | \
CAP_BIT (CAP_CPU_HAS_PUSHXY))
#define CAP_W65C02 \
(CAP_BIT (CAP_CPU_HAS_BRA8) | \
CAP_BIT (CAP_CPU_HAS_INA) | \
CAP_BIT (CAP_CPU_HAS_PUSHXY))
#define CAP_65CE02 \
(CAP_BIT (CAP_CPU_HAS_BRA8) | \
CAP_BIT (CAP_CPU_HAS_INA) | \
CAP_BIT (CAP_CPU_HAS_PUSHXY))
/* Table containing one capability entry per CPU */
static const uint32_t CPUCaps[CPU_COUNT] = {
CAP_NONE, /* CPU_NONE */
CAP_6502, /* CPU_6502 */
CAP_6502X, /* CPU_6502X */
CAP_6502DTV, /* CPU_6502DTV */
CAP_65SC02, /* CPU_65SC02 */
CAP_65C02, /* CPU_65C02 */
CAP_65816, /* CPU_65816 */
CAP_SWEET16, /* CPU_SWEET16 */
CAP_HUC6280, /* CPU_HUC6280 */
CAP_M740, /* CPU_M740 */
CAP_4510, /* CPU_4510 */
CAP_45GS02, /* CPU_45GS02 */
CAP_W65C02, /* CPU_W65C02 */
CAP_65CE02, /* CPU_65CE02 */
};
/*****************************************************************************/
@@ -148,3 +238,12 @@ cpu_t FindCPU (const char* Name)
/* Not found */
return CPU_UNKNOWN;
}
int CPUHasCap (capability_t Cap)
/* Check if the current CPU has the given capability */
{
PRECONDITION (CPU >= 0 && CPU < CPU_COUNT);
return (CPUCaps[CPU] & CAP_BIT (Cap)) != 0;
}

View File

@@ -38,6 +38,11 @@
/* common */
#include "capability.h"
/*****************************************************************************/
/* Data */
/*****************************************************************************/
@@ -64,7 +69,7 @@ typedef enum {
CPU_COUNT /* Number of different CPUs */
} cpu_t;
/* CPU instruction sets (make sure this matches asminc/cpu.mac) */
/* CPU instruction sets */
enum {
CPU_ISET_NONE = 1 << CPU_NONE,
CPU_ISET_6502 = 1 << CPU_6502,
@@ -107,6 +112,9 @@ cpu_t FindCPU (const char* Name);
** the given name is no valid target.
*/
int CPUHasCap (capability_t Cap);
/* Check if the current CPU has the given capability */
/* End of cpu.h */

View File

@@ -37,6 +37,7 @@
#include <string.h>
/* common */
#include "attrib.h"
#include "chartype.h"
#include "check.h"
#include "target.h"
@@ -302,3 +303,12 @@ const char* GetTargetName (target_t Target)
/* Return the array entry */
return GetTargetProperties (Target)->Name;
}
int TargetHasCap (capability_t Cap attribute((unused)))
/* Check if the current target has the given capability */
{
/* Currently unused */
return 0;
}

View File

@@ -39,6 +39,7 @@
/* common */
#include "capability.h"
#include "cpu.h"
@@ -131,6 +132,9 @@ const TargetProperties* GetTargetProperties (target_t Target);
const char* GetTargetName (target_t Target);
/* Return the name of a target */
int TargetHasCap (capability_t Cap);
/* Check if the current target has the given capability */
/* End of target.h */