annotate lwasm/insn_rlist.c @ 510:3425005b9980

Fix segfault if no instances of a section are in the final result If no instances of a section are found to be included in the final linked binary, print a warning instead of crashing with a segfault.
author William Astle <lost@l-w.ca>
date Tue, 02 Jun 2020 21:28:47 -0600
parents f6e03c2cebfb
children f9ffd1935cee
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
1 /*
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
2 insn_rlist.c
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
3 Copyright © 2009 William Astle
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
4
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
5 This file is part of LWASM.
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
6
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
7 LWASM is free software: you can redistribute it and/or modify it under the
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
8 terms of the GNU General Public License as published by the Free Software
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
9 Foundation, either version 3 of the License, or (at your option) any later
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
10 version.
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
11
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
12 This program is distributed in the hope that it will be useful, but WITHOUT
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
15 more details.
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
16
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
17 You should have received a copy of the GNU General Public License along with
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
18 this program. If not, see <http://www.gnu.org/licenses/>.
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
19 */
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
20
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
21 /*
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
22 for handling inherent mode instructions
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
23 */
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
24
2
7317fbe024af Clean up insane number of compiler warnings under -Wall
lost@l-w.ca
parents: 0
diff changeset
25 #include <ctype.h>
7317fbe024af Clean up insane number of compiler warnings under -Wall
lost@l-w.ca
parents: 0
diff changeset
26
0
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
27 #include "lwasm.h"
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
28 #include "instab.h"
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
29
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
30 PARSEFUNC(insn_parse_rlist)
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
31 {
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
32 int rb = 0;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
33 int rn;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
34 static const char *regs = "CCA B DPX Y U PCD S ";
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
35
404
f6e03c2cebfb Fix "rlist" to recognize comments as end of operand
William Astle <lost@l-w.ca>
parents: 402
diff changeset
36 while (**p && !isspace(**p) && **p != ';' && **p != '*')
0
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
37 {
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
38 rn = lwasm_lookupreg2(regs, p);
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
39 if (rn < 0)
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
40 {
370
8764142b3192 Convert internal error/warning handling framework to a new unified system
William Astle <lost@l-w.ca>
parents: 368
diff changeset
41 lwasm_register_error2(as, l, E_REGISTER_BAD, "'%s'", *p);
0
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
42 return;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
43 }
402
b20f14edda5a Completed initial conversion to new parser allowing spaces in operands
William Astle <lost@l-w.ca>
parents: 376
diff changeset
44 lwasm_skip_to_next_token(l, p);
404
f6e03c2cebfb Fix "rlist" to recognize comments as end of operand
William Astle <lost@l-w.ca>
parents: 402
diff changeset
45 if (**p && **p != ',' && !isspace(**p) && **p != ';' && **p != '*')
0
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
46 {
370
8764142b3192 Convert internal error/warning handling framework to a new unified system
William Astle <lost@l-w.ca>
parents: 368
diff changeset
47 lwasm_register_error(as, l, E_OPERAND_BAD);
0
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
48 }
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
49 if (**p == ',')
402
b20f14edda5a Completed initial conversion to new parser allowing spaces in operands
William Astle <lost@l-w.ca>
parents: 376
diff changeset
50 {
0
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
51 (*p)++;
402
b20f14edda5a Completed initial conversion to new parser allowing spaces in operands
William Astle <lost@l-w.ca>
parents: 376
diff changeset
52 lwasm_skip_to_next_token(l, p);
b20f14edda5a Completed initial conversion to new parser allowing spaces in operands
William Astle <lost@l-w.ca>
parents: 376
diff changeset
53 }
373
00fced888c5d Fix psh/pul to not accept the stack pointer as a valid argument
William Astle <lost@l-w.ca>
parents: 370
diff changeset
54 if ((instab[l -> insn].ops[0]) & 2)
00fced888c5d Fix psh/pul to not accept the stack pointer as a valid argument
William Astle <lost@l-w.ca>
parents: 370
diff changeset
55 {
00fced888c5d Fix psh/pul to not accept the stack pointer as a valid argument
William Astle <lost@l-w.ca>
parents: 370
diff changeset
56 // pshu/pulu
00fced888c5d Fix psh/pul to not accept the stack pointer as a valid argument
William Astle <lost@l-w.ca>
parents: 370
diff changeset
57 if (rn == 6)
00fced888c5d Fix psh/pul to not accept the stack pointer as a valid argument
William Astle <lost@l-w.ca>
parents: 370
diff changeset
58 {
00fced888c5d Fix psh/pul to not accept the stack pointer as a valid argument
William Astle <lost@l-w.ca>
parents: 370
diff changeset
59 lwasm_register_error2(as, l, E_REGISTER_BAD, "'%s'", "u");
00fced888c5d Fix psh/pul to not accept the stack pointer as a valid argument
William Astle <lost@l-w.ca>
parents: 370
diff changeset
60 return;
00fced888c5d Fix psh/pul to not accept the stack pointer as a valid argument
William Astle <lost@l-w.ca>
parents: 370
diff changeset
61 }
00fced888c5d Fix psh/pul to not accept the stack pointer as a valid argument
William Astle <lost@l-w.ca>
parents: 370
diff changeset
62 }
00fced888c5d Fix psh/pul to not accept the stack pointer as a valid argument
William Astle <lost@l-w.ca>
parents: 370
diff changeset
63 else
00fced888c5d Fix psh/pul to not accept the stack pointer as a valid argument
William Astle <lost@l-w.ca>
parents: 370
diff changeset
64 {
00fced888c5d Fix psh/pul to not accept the stack pointer as a valid argument
William Astle <lost@l-w.ca>
parents: 370
diff changeset
65 if (rn == 9)
00fced888c5d Fix psh/pul to not accept the stack pointer as a valid argument
William Astle <lost@l-w.ca>
parents: 370
diff changeset
66 {
00fced888c5d Fix psh/pul to not accept the stack pointer as a valid argument
William Astle <lost@l-w.ca>
parents: 370
diff changeset
67 lwasm_register_error2(as, l, E_REGISTER_BAD, "'%s'", "s");
00fced888c5d Fix psh/pul to not accept the stack pointer as a valid argument
William Astle <lost@l-w.ca>
parents: 370
diff changeset
68 return;
00fced888c5d Fix psh/pul to not accept the stack pointer as a valid argument
William Astle <lost@l-w.ca>
parents: 370
diff changeset
69 }
00fced888c5d Fix psh/pul to not accept the stack pointer as a valid argument
William Astle <lost@l-w.ca>
parents: 370
diff changeset
70 }
0
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
71 if (rn == 8)
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
72 rn = 6;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
73 else if (rn == 9)
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
74 rn = 0x40;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
75 else
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
76 rn = 1 << rn;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
77 rb |= rn;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
78 }
368
362f8fb0695b Make psh/pul error out if no registers given
William Astle <lost@l-w.ca>
parents: 2
diff changeset
79 if (rb == 0)
370
8764142b3192 Convert internal error/warning handling framework to a new unified system
William Astle <lost@l-w.ca>
parents: 368
diff changeset
80 lwasm_register_error(as, l, E_OPERAND_BAD);
0
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
81 l -> len = OPLEN(instab[l -> insn].ops[0]) + 1;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
82 l -> pb = rb;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
83 }
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
84
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
85 EMITFUNC(insn_emit_rlist)
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
86 {
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
87 lwasm_emitop(l, instab[l -> insn].ops[0]);
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
88 lwasm_emit(l, l -> pb);
376
35d4213e6657 Add cycle counting to listing
William Astle <lost@l-w.ca>
parents: 373
diff changeset
89
35d4213e6657 Add cycle counting to listing
William Astle <lost@l-w.ca>
parents: 373
diff changeset
90 l -> cycle_adj = lwasm_cycle_calc_rlist(l);
0
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
91 }