-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathlabelcheck.c
More file actions
209 lines (164 loc) · 5.43 KB
/
labelcheck.c
File metadata and controls
209 lines (164 loc) · 5.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
// ソースコードジェネレータ
// ラベルチェック
// Copyright (C) 1989,1990 K.Abe
// All rights reserved.
// Copyright (C) 2025 TcbnErik
#include <stdio.h>
#include "analyze.h"
#include "disasm.h"
#include "estruct.h"
#include "etc.h" /* charout */
#include "global.h"
#include "label.h" /* regist_label etc. */
#include "offset.h" /* depend_address , nearadrs */
static int Label_on_instruction_count = 0;
static int Fixed_count = 0;
static int Undefined_instruction_count = 0;
static void oops_undefined(address pc0, address previous_pc, address nlabel) {
charout('+');
eprintf("\n警告: 未定義命令です(" PRI_ADRS "-" PRI_ADRS ", " PRI_ADRS ")。",
pc0, nlabel, previous_pc);
not_program(pc0, nlabel);
Undefined_instruction_count++;
}
static void printLabelInOpToData(address pc, address nlabel) {
if (!Dis.I) return;
eprintf("\n" PRI_ADRS
": ラベルが命令の中を指すためデータ領域に変更します(" PRI_ADRS
"-" PRI_ADRS ")。",
nlabel, pc, nlabel);
}
static void printLabelInOpShift(address pc, address nlabel) {
if (!Dis.I) return;
eprintf("\n" PRI_ADRS ": 命令の中を指すラベルです(" PRI_ADRS "+$%" PRIx32
")。",
nlabel, pc, (ULONG)(nlabel - pc));
}
static address labelshift(address previous_pc, address pc, address end,
lblbuf* nadrs) {
address nlabel = nadrs->label;
while (nlabel < pc) {
charout('!');
nadrs->shift = nlabel - previous_pc;
regist_label(previous_pc, PROLABEL);
nadrs = Next(nadrs);
nlabel = nadrs->label;
}
if (nlabel >= end) /* 泥縄 (^_^;) */
return next_datlabel(previous_pc)->label;
return end;
}
/*
既に DATLABEL として登録済みかどうか調べる
*/
static boolean datlabel(address adrs) {
lblbuf* ptr = search_label(adrs);
return (ptr && isDATLABEL(ptr->mode)) ? TRUE : FALSE;
}
/*
from から end まで逆アセンブルして命令の途中にアクセスしているところを捜す
*/
static address search_change_operand(address from, address end) {
address previous_pc = from;
DisParam disp;
disasm* code = &disp.code;
boolean was_prog = FALSE; // 直前の領域がプログラムであったかどうか
setDisParamPcPtr(&disp, from, Dis.Ofst);
disp.pcEnd = Dis.availableTextEnd;
while (end != (address)-1 && disp.pc < end) {
lblbuf* nadrs = next(disp.pc + 1);
address nlabel = nadrs->label;
address pc0 = disp.pc;
was_prog = TRUE;
/* 2つのプログラムラベル間を逆アセンブル */
while (disp.pc < nlabel && disp.pc < end) {
previous_pc = disp.pc;
dis(&disp);
if (code->opeType == UNDEF) { /* 未定義命令なら警告 */
oops_undefined(pc0, previous_pc, nlabel);
setDisParamPcPtr(&disp, nlabel, Dis.Ofst);
}
}
/* 命令の途中にアクセスしているなら */
if (disp.pc != nlabel) {
Label_on_instruction_count++;
if (Dis.k ||
(!Dis.E && code->size == BYTESIZE && code->op1.eaadrs + 1 != nlabel &&
code->op2.eaadrs + 1 != nlabel && code->op3.eaadrs + 1 != nlabel &&
code->op4.eaadrs + 1 != nlabel)) {
charout('*');
Fixed_count++;
not_program(pc0, nlabel);
printLabelInOpToData(pc0, nlabel);
setDisParamPcPtr(&disp, nlabel, Dis.Ofst);
was_prog = FALSE;
} else {
end = labelshift(previous_pc, disp.pc, end, nadrs);
printLabelInOpShift(previous_pc, nlabel);
}
}
}
if (code->opeType != RTSOP && code->opeType != JMPOP && datlabel(disp.pc) &&
was_prog) {
charout('-');
regist_label(disp.pc, PROLABEL);
}
return end;
}
/*
from から end までのデータ領域をチェック
*/
static void search_change_data(address from, address end) {
address pc, pc0;
lblbuf* nadrs;
pc = pc0 = from;
nadrs = next(pc);
while (end != (address)-1 && pc < end) {
address dependadrs = nearadrs(pc);
address nlabel;
nadrs = Next(nadrs);
nlabel = nadrs->label;
while (dependadrs != (address)-1 && dependadrs + 4 <= nlabel)
dependadrs = nearadrs(dependadrs + 1);
while (dependadrs < nlabel && nlabel < dependadrs + 4) {
charout('!');
nadrs->shift = nlabel - dependadrs;
regist_label(dependadrs, DATLABEL | (lblmode)UNKNOWN);
nadrs = Next(nadrs);
nlabel = nadrs->label;
}
pc = nlabel;
}
}
extern void search_operand_label(void) {
lblbuf* nadrs = next(Dis.beginTEXT);
address pc, pcend = nadrs->label;
while (nadrs->label < Dis.availableTextEnd) {
charout('.');
nadrs = next_prolabel(pcend);
pc = nadrs->label;
nadrs = next_datlabel(pc);
pcend = nadrs->label;
if (pcend != (address)-1) pcend = search_change_operand(pc, pcend);
}
if (Label_on_instruction_count) {
eprintf("\n命令の中を指すラベル: %d個。", Label_on_instruction_count);
if (Fixed_count)
eprintf("\n%d個の領域をデータ領域に変更しました。", Fixed_count);
}
nadrs = next(Dis.beginTEXT);
pcend = nadrs->label;
while (nadrs->label < Dis.beginBSS) {
charout(':');
nadrs = next_datlabel(pcend);
pc = nadrs->label;
nadrs = next_prolabel(pc);
pcend = MIN(nadrs->label, Dis.beginBSS);
if (pc < Dis.beginBSS) search_change_data(pc, pcend);
}
if (Undefined_instruction_count) {
// ラベルチェックに引っ掛かるとデータ消失 ?
analyze_data();
}
}
// EOF