Skip to content

Commit 7cd892a

Browse files
committed
Attempt at better listing files
1 parent 4ab2c4a commit 7cd892a

File tree

5 files changed

+131
-68
lines changed

5 files changed

+131
-68
lines changed

Changelog.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
Version 7.0.3
22
- Fixed .fpide search so file names are relative to the .fpide file
3+
- Improved listing file tracking of COG pc
34
- Made inline function expansion more robust
45

56
Version 7.0.2

Test/Expect/stest184.pasm

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -579,9 +579,10 @@ _dat_
579579
byte $00[20]
580580
long
581581
__system__dat_
582-
byte $00, $00, $00, $00, $f0, $09, $bc, $0a, $00, $00, $68, $5c, $01, $08, $fc, $0c
583-
byte $03, $08, $7c, $0c, $00, $00, $00, $00, $03, $00, $00, $00, $00, $00, $00, $00
584-
byte $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00
582+
byte $00, $00, $00, $00
583+
byte $f0, $09, $bc, $0a, $00, $00, $68, $5c, $01, $08, $fc, $0c, $03, $08, $7c, $0c
584+
byte $00, $00, $00, $00, $03, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00
585+
byte $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00
585586
__heap_base
586587
long 0[258]
587588
stackspace

backends/asm/assemble_ir.c

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -102,17 +102,6 @@ static const char *RemappedName(const char *name)
102102
return buf;
103103
}
104104

105-
// return \n if the string does not already end in it
106-
static const char *
107-
NeedLineEnd(const char *str) {
108-
const char *r = "\n";
109-
while (*str && str[1]) {
110-
str++;
111-
}
112-
if (*str == '\n') r = "";
113-
return r;
114-
}
115-
116105
// helper function for printing operands
117106
static void
118107
doPrintOperand(struct flexbuf *fb, Operand *reg, int useimm, enum OperandEffect effect_orig, int maximm)
@@ -464,6 +453,25 @@ OutputAlignLong(Flexbuf *fb)
464453
}
465454
}
466455

456+
static void
457+
OutputDebugReloc(Flexbuf *fb, Reloc *nextreloc)
458+
{
459+
LineInfo *info = (LineInfo *)nextreloc->sym;
460+
461+
if (info && info->linedata) {
462+
flexbuf_printf(fb, "'-' ");
463+
// print the line, up to but not including the line end
464+
for (const char *p = info->linedata; *p && *p != '\n'; p++) {
465+
flexbuf_addchar(fb, *p);
466+
}
467+
if (nextreloc->symoff) {
468+
// feed some data through
469+
flexbuf_printf(fb, " {#pragma cogval $%08x}", nextreloc->symoff);
470+
}
471+
flexbuf_addchar(fb, '\n');
472+
}
473+
}
474+
467475
void
468476
OutputDataBlob(Flexbuf *fb, Flexbuf *databuf, Flexbuf *relocbuf, const char *startLabel, bool outLabel)
469477
{
@@ -514,11 +522,7 @@ OutputDataBlob(Flexbuf *fb, Flexbuf *databuf, Flexbuf *relocbuf, const char *sta
514522

515523
// we have to output a relocation or debug entry now
516524
if (nextreloc->kind == RELOC_KIND_DEBUG) {
517-
LineInfo *info = (LineInfo *)nextreloc->sym;
518-
if (info && info->linedata) {
519-
const char *lineend = NeedLineEnd(info->linedata);
520-
flexbuf_printf(fb, "'-' %s%s", info->linedata, lineend);
521-
}
525+
OutputDebugReloc(fb, nextreloc);
522526
nextreloc++;
523527
--relocs;
524528
goto again;
@@ -666,11 +670,7 @@ OutputDataBlob(Flexbuf *fb, Flexbuf *databuf, Flexbuf *relocbuf, const char *sta
666670
}
667671
while (relocs > 0 && nextreloc) {
668672
if (nextreloc->kind == RELOC_KIND_DEBUG) {
669-
LineInfo *info = (LineInfo *)nextreloc->sym;
670-
if (info && info->linedata) {
671-
const char *lineend = NeedLineEnd(info->linedata);
672-
flexbuf_printf(fb, "'-' %s%s", info->linedata, lineend);
673-
}
673+
OutputDebugReloc(fb, nextreloc);
674674
}
675675
nextreloc++;
676676
--relocs;

backends/dat/outdat.c

Lines changed: 42 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1365,20 +1365,40 @@ DecodeAsmOperands(Instruction *instr, AST *ast, AST **operand, uint32_t *opimm,
13651365
return numoperands;
13661366
}
13671367

1368+
/*
1369+
* put a single commented source line in relocs
1370+
*/
1371+
static void
1372+
SendOneComment(Flexbuf *relocs, AST *ast, uint32_t addr, uint32_t info)
1373+
{
1374+
Reloc r;
1375+
Reloc *oldr;
1376+
if (relocs) {
1377+
// see if this is just adding some info to a previous line
1378+
if (!ast) {
1379+
if (flexbuf_curlen(relocs) > sizeof(Reloc) && info) {
1380+
oldr = (Reloc *)(flexbuf_peek(relocs) + flexbuf_curlen(relocs) - sizeof(Reloc));
1381+
if (oldr->kind == RELOC_KIND_DEBUG && oldr->sym && !oldr->symoff) {
1382+
oldr->symoff = info;
1383+
return;
1384+
}
1385+
}
1386+
}
1387+
r.kind = RELOC_KIND_DEBUG;
1388+
r.addr = addr;
1389+
r.sym = ast ? (Symbol *)GetLineInfo(ast) : 0;
1390+
r.symoff = info;
1391+
flexbuf_addmem(relocs, (const char *)&r, sizeof(r));
1392+
}
1393+
}
1394+
13681395
// assemble comments
13691396
// returns first non-comment thing seen
13701397
static AST* AssembleComments(Flexbuf *f, Flexbuf *relocs, AST *ast)
13711398
{
1372-
Reloc r;
13731399
while (ast) {
13741400
if (ast->kind == AST_SRCCOMMENT && gl_srccomments) {
1375-
if (relocs) {
1376-
r.kind = RELOC_KIND_DEBUG;
1377-
r.addr = flexbuf_curlen(f);
1378-
r.sym = (Symbol *)GetLineInfo(ast);
1379-
r.symoff = 0;
1380-
flexbuf_addmem(relocs, (const char *)&r, sizeof(r));
1381-
}
1401+
SendOneComment(relocs, ast, flexbuf_curlen(f), 0);
13821402
} else if (ast->kind == AST_COMMENT) {
13831403
/* do nothing */
13841404
} else {
@@ -1977,22 +1997,6 @@ padBytes(Flexbuf *f, AST *ast, int bytes)
19771997
}
19781998
}
19791999

1980-
/*
1981-
* put a single commented source line in relocs
1982-
*/
1983-
static void
1984-
SendOneComment(Flexbuf *relocs, AST *ast, uint32_t addr)
1985-
{
1986-
Reloc r;
1987-
if (relocs && ast) {
1988-
r.kind = RELOC_KIND_DEBUG;
1989-
r.addr = addr;
1990-
r.sym = (Symbol *)GetLineInfo(ast);
1991-
r.symoff = 0;
1992-
flexbuf_addmem(relocs, (const char *)&r, sizeof(r));
1993-
}
1994-
}
1995-
19962000
/*
19972001
* send out all comments pending, and return the next non-comment node
19982002
*/
@@ -2005,7 +2009,7 @@ SendComments(Flexbuf *f, AST *ast, Flexbuf *relocs)
20052009
/* ignore, for now */
20062010
break;
20072011
case AST_SRCCOMMENT:
2008-
SendOneComment(relocs, ast, flexbuf_curlen(f));
2012+
SendOneComment(relocs, ast, flexbuf_curlen(f), 0);
20092013
break;
20102014
default:
20112015
return ast;
@@ -2140,6 +2144,7 @@ PrintDataBlock(Flexbuf *f, AST *list, DataBlockOutFuncs *funcs, Flexbuf *relocs)
21402144
if (!gl_nospin && ast->d.ival > 3 && gl_output != OUTPUT_DAT) {
21412145
WARNING(ast, "orgh with explicit origin does not work if Spin methods are present");
21422146
}
2147+
SendOneComment(relocs, NULL, flexbuf_curlen(f), EvalPasmExpr(ast->left));
21432148
/* skip ahead to PC */
21442149
padBytes(f, ast, ast->d.ival);
21452150
inHub = 1;
@@ -2149,27 +2154,34 @@ PrintDataBlock(Flexbuf *f, AST *list, DataBlockOutFuncs *funcs, Flexbuf *relocs)
21492154
/* need to skip ahead to PC */
21502155
padBytes(f, ast, ast->d.ival);
21512156
inHub = 0;
2157+
// SendOneComment(relocs, NULL, flexbuf_curlen(f), EvalPasmExpr(ast->left));
21522158
break;
2153-
case AST_ORG:
2159+
case AST_ORG: {
2160+
int cog_data = EvalPasmExpr(ast->left);
21542161
inHub = 0;
21552162
if (NEED_ALIGNMENT) {
21562163
AlignPc(f, 4);
21572164
}
2165+
SendOneComment(relocs, NULL, flexbuf_curlen(f), cog_data);
21582166
break;
2159-
case AST_RES:
2160-
/* don't need to emit anything */
2167+
}
2168+
case AST_RES: {
2169+
int cog_data = EvalPasmExpr(ast->left);
2170+
/* don't need to emit anything, but send a debug comment */
2171+
SendOneComment(relocs, NULL, flexbuf_curlen(f), cog_data);
21612172
break;
2173+
}
21622174
case AST_FIT:
21632175
// emit a debug entry
2164-
SendOneComment(relocs, ast, flexbuf_curlen(f));
2176+
SendOneComment(relocs, ast, flexbuf_curlen(f), EvalPasmExpr(ast->left));
21652177
break;
21662178
case AST_LINEBREAK:
21672179
break;
21682180
case AST_COMMENT:
21692181
break;
21702182
case AST_SRCCOMMENT:
21712183
// emit a debug entry
2172-
SendOneComment(relocs, ast, flexbuf_curlen(f));
2184+
SendOneComment(relocs, ast, flexbuf_curlen(f), 0);
21732185
break;
21742186
default:
21752187
ERROR(ast, "unknown element in data block");

backends/dat/outlst.c

Lines changed: 63 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//
22
// listing file output for spin2cpp
33
//
4-
// Copyright 2012-2023 Total Spectrum Software Inc.
4+
// Copyright 2012-2025 Total Spectrum Software Inc.
55
// see the file COPYING for conditions of redistribution
66
//
77
#include <stdio.h>
@@ -80,21 +80,76 @@ static void AddRestOfLine(Flexbuf *f, const char *s) {
8080
flexbuf_printf(f, "| ");
8181
while (*s) {
8282
c = *s++;
83+
if (c == '{' && *s == '#') {
84+
if (!strncmp(s, "#pragma cogval ", strlen("#pragma cogval "))) {
85+
// just abort now
86+
break;
87+
}
88+
}
8389
if (c == '\n') {
8490
break;
8591
}
8692
flexbuf_addchar(f, c);
8793
}
8894
}
8995

96+
static bool isFirstLabelChar(int c) {
97+
if (c == '_' || c == ':') return true;
98+
return isalpha(c);
99+
}
100+
static bool isLabelChar(int c) {
101+
if (c == '_' || c == ':') return true;
102+
return isalnum(c);
103+
}
104+
105+
static unsigned fetchOrgValue(const char *s) {
106+
int base = 0;
107+
unsigned val = 0;
108+
const char *p;
109+
110+
// scan ahead to see if there is a {#pragma cogval}
111+
// to give us the true (calculated) value
112+
p = strstr(s, "{#pragma cogval ");
113+
if (p) {
114+
s = p + strlen("{#pragma cogval ");
115+
}
116+
if (*s == '$') {
117+
base = 16;
118+
s++;
119+
}
120+
if (*s) {
121+
if (isFirstLabelChar(*s)) {
122+
char *sdup = strdup(s);
123+
char *p = sdup;
124+
Symbol *sym;
125+
while (isLabelChar(*p)) p++;
126+
*p = 0;
127+
sym = LookupSymbol(sdup);
128+
if (sym && sym->kind == SYM_CONSTANT) {
129+
val = EvalConstExpr((AST *)sym->v.ptr);
130+
}
131+
free(sdup);
132+
} else {
133+
val = strtoul(s, NULL, base);
134+
}
135+
}
136+
return val;
137+
}
138+
90139
// check for Org in DAT output
91140
// DAT sections get compiled to binary blobs with '-' comments giving the
92141
// original code; but in listings we want COG addresses to show up, so we
93-
// need to look for ORG / ORGH
142+
// need to look for special directives. The reloc info may append
143+
// additional data in {#pragma cogval} comments to help us
144+
//
94145
static void CheckForOrg(const char *theline)
95146
{
96147
const char *s = theline;
97148

149+
//printf("CheckForOrg: [%s]\n", theline);
150+
151+
// skip over leading label, if any
152+
while (*s && isLabelChar(*s)) s++;
98153
while (*s && isspace(*s)) s++;
99154
if (*s == '\'' || *s == '{') return;
100155
if (!strncasecmp(s, "org", 3)) {
@@ -103,19 +158,13 @@ static void CheckForOrg(const char *theline)
103158
} else if (!s[3] || isspace(s[3])) {
104159
s += 3;
105160
if (isspace(*s)) s++;
106-
// Aargh, need to find the actual COG PC
107-
while (*s && *s != '$') s++;
108-
if (*s == '$') {
109-
s++;
110-
if (*s) {
111-
cogPc = strtoul(s, NULL, 16)*4;
112-
inCog = 1;
113-
}
114-
} else if (!*s) {
115-
cogPc = 0;
116-
inCog = 1;
117-
}
161+
// find the actual COG PC
162+
cogPc = fetchOrgValue(s) * 4;
163+
inCog = 1;
118164
}
165+
} else if (!strncasecmp(s, "res", 3) && isspace(s[3])) {
166+
s += 4;
167+
cogPc += fetchOrgValue(s) * 4;
119168
}
120169
}
121170

0 commit comments

Comments
 (0)