-
Notifications
You must be signed in to change notification settings - Fork 1
/
Fl_Text_Editor_Asm.cxx
343 lines (298 loc) · 9.88 KB
/
Fl_Text_Editor_Asm.cxx
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
/* FL_Text_Editor with assembly syntax highlighting */
#include <string>
#include <cctype>
#include <locale>
using namespace std;
#include <FL/Fl.H>
#include <FL/x.H> // for fl_open_callback
#include <FL/Fl_Group.H>
#include <FL/Fl_Double_Window.H>
#include <FL/fl_ask.H>
#include <FL/Fl_Native_File_Chooser.H>
#include <FL/Fl_Menu_Bar.H>
#include <FL/Fl_Input.H>
#include <FL/Fl_Button.H>
#include <FL/Fl_Return_Button.H>
#include <FL/Fl_Text_Buffer.H>
#include <FL/Fl_Text_Editor.H>
#include <FL/filename.H>
#include "Fl_Text_Editor_Asm.h"
/*****************************************************************************/
/* syntax stuff (can exist outside the class, takes only char *'s) */
/*****************************************************************************/
#define TS 14 // default editor textsize
static
Fl_Text_Editor::Style_Table_Entry
styletable[] = { // Style table
{ FL_BLACK, FL_COURIER, TS }, // A - Plain
{ FL_DARK_GREEN, FL_HELVETICA, TS }, // B - Line comments
{ FL_DARK_BLUE, FL_COURIER, TS }, // C - numbers
{ FL_DARK_CYAN, FL_COURIER_BOLD, TS }, // D - Labels
{ FL_DARK_RED, FL_COURIER, TS }, // E - Directives
{ FL_DARK_RED, FL_COURIER, TS }, // F - Registers
{ FL_BLUE, FL_COURIER_BOLD, TS }, // G - Keywords
};
//const char *x86_mnemonics[] = {
//
//};
#define ARRAY_LEN(X) (sizeof(X)/sizeof(*X))
extern "C" { /* so bswap will take it */
static
int
compare_keywords(const void *a, const void *b)
{
return (strcmp(*((const char **)a), *((const char **)b)));
}
}
/*****************************************************************************/
/* X86 style parsing (at&t) */
/*****************************************************************************/
// (sorted for bsearch())
const char *x86_regs_att[] = {
"%eax", "%ebp", "%ebx", "%ecx", "%edi", "%edx", "%esi",
"%rax", "%rbp", "%rbx", "%rcx", "%rdi", "%rdx", "%rip", "%rsi", "%rsp"
};
extern "C" { /* so bswap will take it */
static
int
compare_x86_regs_att(const void *a_, const void *b_)
{
char *a = (char *)a_;
char *b = *(char **)b_;
//printf("comparing -%c%c%c%c- to -%c%c%c%c-\n",
// a[0], a[1], a[2], a[3], b[0], b[1], b[2], b[3]);
return strncmp((const char *)a, (const char *)b, 4);
}
}
// 'style_parse()' - Parse text and produce style data.
static
void
style_parse_x86_att(const char *text, char *style, int length)
{
for(int i=0; i<length; ) {
bool boring = true;
/* pick out registers */
if(length-i >= 4 && text[0] != ' ') {
if(bsearch(text+i, &(x86_regs_att[0]), ARRAY_LEN(x86_regs_att), sizeof(char *), compare_x86_regs_att)) {
strncpy(style+i, "FFFF", 4);
i += 4;
boring = false;
}
}
/* pick out numbers */
if(boring && length-i >= 3 && text[i]=='0' && text[i+1]=='x' && isxdigit(text[i+2])) {
int numLen = 3;
while(isxdigit(text[i+numLen++]));
numLen -= 1;
memset(style+i, 'C', numLen);
i += numLen;
boring = false;
}
/* pick out line comments */
if(boring && text[i]=='#') {
int nlLen = strlen("\n");
/* end-to-line comment, seek to newline or end of buffer */
int commLen = 1;
for(int j=i+1; j<length; ++j)
if(0==strncmp(text+j, "\n", nlLen))
break;
else
commLen++;
memset(style+i, 'B', commLen);
i += commLen;
boring = false;
}
/* pick out labels */
if(boring && (length-i >= 3) && text[i] != ' ') {
const char *b = strstr(text+i, "\n");
if(b && b!=(text+i) && b[-1] == ':') {
int labelLen = b - (text+i);
//printf("labelLen is %d\n", labelLen);
memset(style+i, 'D', labelLen);
i += labelLen;
boring = false;
}
}
if(boring) {
style[i++] = 'A';
}
}
}
static
void
style_parse_x86_intel(const char *text, char *style, int length)
{
for(int i=0; i<length; ) {
bool boring = true;
/* pick out registers */
if(length-i >= 4 && text[0] != ' ') {
if(bsearch(text+i, &(x86_regs_att[0]), ARRAY_LEN(x86_regs_att), sizeof(char *), compare_x86_regs_att)) {
strncpy(style+i, "FFFF", 4);
i += 4;
boring = false;
}
}
/* pick out numbers */
if(boring && length-i >= 3 && text[i]=='0' && text[i+1]=='x' && isxdigit(text[i+2])) {
int numLen = 3;
while(isxdigit(text[i+numLen++]));
numLen -= 1;
memset(style+i, 'C', numLen);
i += numLen;
boring = false;
}
/* pick out line comments */
if(boring && text[i]=='#') {
int nlLen = strlen("\n");
/* end-to-line comment, seek to newline or end of buffer */
int commLen = 1;
for(int j=i+1; j<length; ++j)
if(0==strncmp(text+j, "\n", nlLen))
break;
else
commLen++;
memset(style+i, 'B', commLen);
i += commLen;
boring = false;
}
/* pick out labels */
if(boring && (length-i >= 3) && text[i] != ' ') {
const char *b = strstr(text+i, "\n");
if(b && b!=(text+i) && b[-1] == ':') {
int labelLen = b - (text+i);
//printf("labelLen is %d\n", labelLen);
memset(style+i, 'D', labelLen);
i += labelLen;
boring = false;
}
}
if(boring) {
style[i++] = 'A';
}
}
}
// 'style_unfinished_cb()' - Update unfinished styles.
static
void
style_unfinished_cb(int, void*)
{
return;
}
static
void
source_modified_cb(int a, int b, int c, int d, const char *e, void *cbArg)
{
Fl_Text_Editor_Asm *widget = (Fl_Text_Editor_Asm *)cbArg;
widget->onSrcMod(a, b, c, d, e, cbArg);
}
/*****************************************************************************/
/* class stuff */
/*****************************************************************************/
Fl_Text_Editor_Asm::Fl_Text_Editor_Asm(int x, int y, int w, int h):
Fl_Text_Editor(x, y, w, h) {
m_styleBuf = new Fl_Text_Buffer();
m_x86_flavor = X86_FLAVOR_INTEL;
m_autoSyntaxHighlight = 1;
m_styleFunc = style_parse_x86_intel;
}
/* we intercept any replacement of the buffer to allocate a parallel style
buffer and add our style callback */
void Fl_Text_Editor_Asm::buffer(Fl_Text_Buffer *buf)
{
Fl_Text_Display::buffer(buf);
styleRealloc();
buf->add_modify_callback(source_modified_cb, this);
Fl_Text_Editor::highlight_data(m_styleBuf, styletable, 7, 'X', NULL, NULL);
}
void Fl_Text_Editor_Asm::buffer(Fl_Text_Buffer &buf)
{
Fl_Text_Display::buffer(buf);
styleRealloc();
buf.add_modify_callback(source_modified_cb, this);
Fl_Text_Editor::highlight_data(m_styleBuf, styletable, 7, 'X', NULL, NULL);
}
void Fl_Text_Editor_Asm::synHighlightNow()
{
styleRealloc();
Fl_Text_Buffer *source = Fl_Text_Display::buffer();
// innefficient to do this everytime, but who cares for now
styleRealloc();
}
// this is the callback when the source buffer is modified
//
// typedef void (*Fl_Text_Modify_Cb)(int pos, int nInserted, int nDeleted,
// int nRestyled, const char* deletedText, void* cbArg);
//
void Fl_Text_Editor_Asm::onSrcMod(int pos, // Position of update
int nInserted, // Number of inserted chars
int nDeleted, // Number of deleted chars
int nRestyled, // Number of restyled chars
const char *deletedText, // Text that was deleted
void *cbArg) // Callback data
{
/* get the text buffer we are enveloping */
Fl_Text_Buffer *source = Fl_Text_Display::buffer();
if (nInserted == 0 && nDeleted == 0) {
printf("none inserted, none deleted, just unselecting the style buff\n");
m_styleBuf->unselect();
return;
}
if(m_autoSyntaxHighlight) {
synHighlightNow();
};
}
void Fl_Text_Editor_Asm::styleRealloc()
{
Fl_Text_Buffer *ftb = Fl_Text_Display::buffer();
if(!ftb) {
printf("ERROR: no buffer set!?\n");
}
else {
// allocate style bytes
char *style = new char[ftb->length() + 1];
// fill style bytes with first style entry
memset(style, 'A', ftb->length());
style[ftb->length()] = '\0';
// alloc a char* for non-oop style function
char *source = ftb->text();
m_styleFunc(source, style, ftb->length());
free(source);
// set the m_styleBuf to the style bytes
m_styleBuf->text(style);
delete[] style;
}
}
void Fl_Text_Editor_Asm::styleSet(int style)
{
switch(style) {
case STYLE_X86_ATT:
m_styleFunc = style_parse_x86_intel;
break;
case STYLE_X86_INTEL:
m_styleFunc = style_parse_x86_intel;
break;
case STYLE_X86_64_ATT:
m_styleFunc = style_parse_x86_intel;
break;
case STYLE_X86_64_INTEL:
m_styleFunc = style_parse_x86_intel;
break;
case STYLE_X86_ARM:
m_styleFunc = style_parse_x86_intel;
break;
case STYLE_X86_THUMB:
m_styleFunc = style_parse_x86_intel;
break;
case STYLE_X86_AARCH64:
m_styleFunc = style_parse_x86_intel;
break;
case STYLE_X86_PPC:
m_styleFunc = style_parse_x86_intel;
break;
case STYLE_X86_PPC64:
m_styleFunc = style_parse_x86_intel;
break;
default:
printf("ERROR: unknown style id: %d\n", style);
}
}