Skip to content

Commit 36848eb

Browse files
committed
better error checking for inline assembly being too large
1 parent 54ea3ff commit 36848eb

File tree

4 files changed

+35
-16
lines changed

4 files changed

+35
-16
lines changed

backends/asm/inlineasm.c

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -888,12 +888,13 @@ CompileMiniDatBlock(IRList *irl, AST *origtop, unsigned asmFlags)
888888
AST *top = origtop;
889889
IR *fcache;
890890
IR *startlabel, *endlabel;
891-
IR *ir, *fitir;
891+
IR *ir;
892892
Operand *enddst, *startdst;
893893
Flexbuf *fb = (Flexbuf *)malloc(sizeof(Flexbuf));
894894
Flexbuf *relocs = (Flexbuf *)malloc(sizeof(Flexbuf));
895895
struct CopyLocalData copyInfo;
896-
896+
PASMAddresses paddr;
897+
897898
flexbuf_init(fb, 1024);
898899
flexbuf_init(relocs, 1024);
899900

@@ -912,7 +913,11 @@ CompileMiniDatBlock(IRList *irl, AST *origtop, unsigned asmFlags)
912913

913914
/* compile the inline assembly as a DAT block */
914915
curfunc->localsUsedInAsm = 0;
915-
AssignAddresses(&curfunc->localsyms, top, 0);
916+
AssignAddresses(&paddr, &curfunc->localsyms, top, 0);
917+
if (paddr.cogPc > gl_fcache_size * LONG_SIZE) {
918+
ERROR(origtop, "Inline assembly is too large to fit in %d longs\n",
919+
gl_fcache_size);
920+
}
916921
PrintDataBlock(fb, top, NULL, relocs);
917922

918923
/* copy the locals used in the inline assembly */
@@ -934,10 +939,6 @@ CompileMiniDatBlock(IRList *irl, AST *origtop, unsigned asmFlags)
934939
ir->src2 = (Operand *)current;
935940

936941
/* finish off the block */
937-
fitir = NewIR(OPC_FIT);
938-
fitir->dst = NewImmediate(gl_fcache_size);
939-
fitir->flags |= FLAG_USER_INSTR;
940-
AppendIR(irl, fitir);
941942
AppendIR(irl, endlabel);
942943
if (gl_p2) {
943944
IR *orgh = NewIR(OPC_HUBMODE);

backends/nucode/outnu.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1917,14 +1917,19 @@ static void NuCompileInlineAsm(NuIrList *irl, AST *ast) {
19171917
NuIrLabel *startLabel;
19181918
NuFunData *fdata = FunData(curfunc);
19191919
unsigned startPos, endPos;
1920-
1920+
PASMAddresses paddr;
1921+
19211922
if (!fdata->dataLabel) {
19221923
fdata->dataLabel = NuCreateLabel();
19231924
flexbuf_init(&fdata->dataBuf, 1024);
19241925
}
19251926
startPos = flexbuf_curlen(&fdata->dataBuf);
19261927
startLabel = NuIrOffsetLabel(fdata->dataLabel, startPos);
1927-
AssignAddresses(&curfunc->localsyms, list, 0);
1928+
AssignAddresses(&paddr, &curfunc->localsyms, list, 0);
1929+
if (paddr.cogPc > gl_fcache_size * LONG_SIZE) {
1930+
ERROR(ast, "Inline assembly is too large to fit in %d longs\n",
1931+
gl_fcache_size);
1932+
}
19281933
PrintDataBlock(&fdata->dataBuf,list,NULL,&fdata->dataBufRelocs);
19291934
// append a RET instruction
19301935
// which is $FD64002D

frontends/common.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -672,8 +672,15 @@ bool IsSystemModule(Module *P);
672672
/* create a new AST describing a table lookup */
673673
AST *NewLookup(AST *expr, AST *table);
674674

675+
typedef struct PASMAddresses {
676+
unsigned dataSize; /* total data size */
677+
unsigned cogPc; /* last cog PC */
678+
unsigned hubPc; /* last (relative) hub PC */
679+
} PASMAddresses;
680+
675681
/* declare labels in PASM */
676-
unsigned AssignAddresses(SymbolTable *symtab, AST *instrlist, int startFlags);
682+
/* fills out a struct giving the various maximum pc's and sizes */
683+
void AssignAddresses(PASMAddresses *addr, SymbolTable *symtab, AST *instrlist, int startFlags);
677684
void DeclareModuleLabels(Module *);
678685
#define ADDRESS_STARTFLAG_COG 0x0
679686
#define ADDRESS_STARTFLAG_HUB 0x1

pasm.c

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
* PASM and data compilation
3-
* Copyright 2011-2023 Total Spectrum Software Inc.
3+
* Copyright 2011-2025 Total Spectrum Software Inc.
44
*
55
* +--------------------------------------------------------------------
66
* ¦ TERMS OF USE: MIT License
@@ -657,8 +657,8 @@ IsJmpRetInstruction(AST *ast)
657657
return false;
658658
}
659659

660-
unsigned
661-
AssignAddresses(SymbolTable *symtab, AST *instrlist, int startFlags)
660+
void
661+
AssignAddresses(PASMAddresses *addr, SymbolTable *symtab, AST *instrlist, int startFlags)
662662
{
663663
unsigned cogpc = 0;
664664
unsigned hubpc = 0;
@@ -1025,20 +1025,26 @@ AssignAddresses(SymbolTable *symtab, AST *instrlist, int startFlags)
10251025
orig_datoff = datoff;
10261026
goto again;
10271027
}
1028-
return datoff;
1028+
if (addr) {
1029+
addr->dataSize = datoff;
1030+
addr->hubPc = hubpc;
1031+
addr->cogPc = cogpc;
1032+
}
10291033
}
10301034

10311035
void
10321036
DeclareModuleLabels(Module *P)
10331037
{
10341038
Module *save = current;
10351039
int startFlags = 0;
1036-
1040+
PASMAddresses paddr;
1041+
10371042
if (gl_no_coginit && gl_output == OUTPUT_DAT) {
10381043
startFlags = ADDRESS_STARTFLAG_HUB;
10391044
}
10401045
current = P;
10411046
P->gasPasm = gl_gas_dat;
1042-
P->datsize = AssignAddresses(&P->objsyms, P->datblock, startFlags);
1047+
AssignAddresses(&paddr, &P->objsyms, P->datblock, startFlags);
1048+
P->datsize = paddr.dataSize;
10431049
current = save;
10441050
}

0 commit comments

Comments
 (0)