/
spim-utils.c
598 lines (470 loc) · 13 KB
/
spim-utils.c
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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
/* SPIM S20 MIPS simulator.
Misc. routines for SPIM.
Copyright (c) 1990-2010, James R. Larus.
All rights reserved.
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation and/or
other materials provided with the distribution.
Neither the name of the James R. Larus nor the names of its contributors may be
used to endorse or promote products derived from this software without specific
prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <stdarg.h>
#include "spim.h"
#include "string-stream.h"
#include "spim-utils.h"
#include "inst.h"
#include "data.h"
#include "reg.h"
#include "mem.h"
#include "scanner.h"
#include "parser.h"
#include "y.tab.h"
#include "run.h"
#include "sym-tbl.h"
/* Internal functions: */
static mem_addr copy_int_to_stack (int n);
static mem_addr copy_str_to_stack (char *s);
static void delete_all_breakpoints ();
int exception_occurred;
mem_addr program_starting_address = 0;
int initial_text_size = TEXT_SIZE;
int initial_data_size = DATA_SIZE;
mem_addr initial_data_limit = DATA_LIMIT;
int initial_stack_size = STACK_SIZE;
mem_addr initial_stack_limit = STACK_LIMIT;
int initial_k_text_size = K_TEXT_SIZE;
int initial_k_data_size = K_DATA_SIZE;
mem_addr initial_k_data_limit = K_DATA_LIMIT;
/* Initialize or reinitialize the state of the machine. */
void
initialize_world (char* exception_file_names)
{
/* Allocate the floating point registers */
if (FGR == NULL)
FPR = (double *) xmalloc (FPR_LENGTH * sizeof (double));
/* Allocate the memory */
make_memory (initial_text_size,
initial_data_size, initial_data_limit,
initial_stack_size, initial_stack_limit,
initial_k_text_size,
initial_k_data_size, initial_k_data_limit);
initialize_registers ();
program_starting_address = 0;
initialize_inst_tables ();
initialize_symbol_table ();
k_text_begins_at_point (K_TEXT_BOT);
k_data_begins_at_point (K_DATA_BOT);
data_begins_at_point (DATA_BOT);
text_begins_at_point (TEXT_BOT);
if (exception_file_names != NULL)
{
int old_bare = bare_machine;
int old_accept = accept_pseudo_insts;
char *filename;
char *files;
/* Save machine state */
bare_machine = 0; /* Exception handler uses extended machine */
accept_pseudo_insts = 1;
/* strtok modifies the string, so we must back up the string prior to use. */
if ((files = strdup (exception_file_names)) == NULL)
fatal_error ("Insufficient memory to complete.\n");
for (filename = strtok (files, ";"); filename != NULL; filename = strtok (NULL, ";"))
{
if (read_assembly_file (filename))
fatal_error ("Cannot read exception handler: %s\n", filename);
write_output (message_out, "Loaded: %s\n", filename);
}
free (files);
/* Restore machine state */
bare_machine = old_bare;
accept_pseudo_insts = old_accept;
if (!bare_machine)
{
(void)make_label_global ("main"); /* In case .globl main forgotten */
(void)record_label ("main", 0, 0);
}
}
initialize_scanner (stdin);
delete_all_breakpoints ();
}
void
write_startup_message ()
{
write_output (message_out, "SPIM %s\n", SPIM_VERSION);
write_output (message_out, "Copyright 1990-2010, James R. Larus.\n");
write_output (message_out, "All Rights Reserved.\n");
#ifdef WIN32
write_output (message_out, "DOS and Windows ports by David A. Carley.\n");
write_output (message_out, "Copyright 1997, Morgan Kaufmann Publishers, Inc.\n");
#endif
write_output (message_out, "See the file README for a full copyright notice.\n");
}
void
initialize_registers ()
{
memclr (FPR, FPR_LENGTH * sizeof (double));
FGR = (float *) FPR;
FWR = (int *) FPR;
memclr (R, R_LENGTH * sizeof (reg_word));
R[REG_SP] = STACK_TOP - BYTES_PER_WORD - 4096; /* Initialize $sp */
HI = LO = 0;
PC = 0;
CP0_BadVAddr = 0;
CP0_Count = 0;
CP0_Compare = 0;
CP0_Status = (CP0_Status_CU & 0x30000000) | CP0_Status_IM | CP0_Status_UM;
CP0_Cause = 0;
CP0_EPC = 0;
#ifdef BIGENDIAN
CP0_Config = CP0_Config_BE;
#else
CP0_Config = 0;
#endif
FIR = FIR_W | FIR_D | FIR_S; /* Word, double, & single implemented */
FCSR = 0x0;
FCCR = 0x0;
FEXR = 0x0;
FENR = 0x0;
}
/* Read file NAME, which should contain assembly code. Return zero if
successful and non-zero otherwise. */
int
read_assembly_file (char *name)
{
FILE *file = fopen (name, "rt");
if (file == NULL)
{
error ("Cannot open file: `%s'\n", name);
return (1);
}
else
{
initialize_scanner (file);
initialize_parser (name);
while (!yyparse ()) ;
fclose (file);
flush_local_labels (!parse_error_occurred);
end_of_assembly_file ();
return (0);
}
}
mem_addr
starting_address ()
{
if (PC == 0)
{
if (program_starting_address != 0)
return (program_starting_address);
else
return (program_starting_address
= find_symbol_address (DEFAULT_RUN_LOCATION));
}
else
return (PC);
}
/* Initialize the SPIM stack with ARGC, ARGV, and ENVP data. */
#ifdef _MSC_VER
#define environ _environ
#endif
void
initialize_run_stack (int argc, char **argv)
{
char **p;
extern char **environ;
int i, j = 0, env_j;
mem_addr addrs[10000];
R[REG_SP] = STACK_TOP - 1; /* Initialize $sp */
/* Put strings on stack: */
/* env: */
for (p = environ; *p != NULL; p++)
addrs[j++] = copy_str_to_stack (*p);
env_j = j;
/* argv; */
for (i = 0; i < argc; i++)
addrs[j++] = copy_str_to_stack (argv[i]);
/* Align stack pointer for word-size data */
R[REG_SP] = R[REG_SP] & ~3; /* Round down to nearest word */
R[REG_SP] -= BYTES_PER_WORD; /* First free word on stack */
R[REG_SP] = R[REG_SP] & ~7; /* Double-word align stack-pointer*/
/* Build vectors on stack: */
/* env: */
(void)copy_int_to_stack (0); /* Null-terminate vector */
for (i = env_j - 1; i >= 0; i--)
R[REG_A2] = copy_int_to_stack (addrs[i]);
/* argv: */
(void)copy_int_to_stack (0); /* Null-terminate vector */
for (i = j - 1; i >= env_j; i--)
R[REG_A1] = copy_int_to_stack (addrs[i]);
/* argc: */
R[REG_A0] = argc;
set_mem_word (R[REG_SP], argc); /* Leave argc on stack */
}
static mem_addr
copy_str_to_stack (char *s)
{
int i = strlen (s);
while (i >= 0)
{
set_mem_byte (R[REG_SP], s[i]);
R[REG_SP] -= 1;
i -= 1;
}
return ((mem_addr) R[REG_SP] + 1); /* Leaves stack pointer byte-aligned!! */
}
static mem_addr
copy_int_to_stack (int n)
{
set_mem_word (R[REG_SP], n);
R[REG_SP] -= BYTES_PER_WORD;
return ((mem_addr) R[REG_SP] + BYTES_PER_WORD);
}
/* Run a program starting at PC for N steps and display each
instruction before executing if FLAG is non-zero. If CONTINUE is
non-zero, then step through a breakpoint. Return non-zero if
breakpoint is encountered. */
int
run_program (mem_addr pc, int steps, int display, int cont_bkpt)
{
if (cont_bkpt && inst_is_breakpoint (pc))
{
mem_addr addr = PC == 0 ? pc : PC;
delete_breakpoint (addr);
exception_occurred = 0;
run_spim (addr, 1, display);
add_breakpoint (addr);
steps -= 1;
pc = PC;
}
exception_occurred = 0;
if (!run_spim (pc, steps, display))
/* Can't restart program */
PC = 0;
if (exception_occurred && CP0_ExCode == ExcCode_Bp)
{
/* Turn off EXL bit, so subsequent interrupts set EPC since the break is
handled by SPIM code, not MIPS code. */
CP0_Status &= ~CP0_Status_EXL;
return (1);
}
else
return (0);
}
/* Record of where a breakpoint was placed and the instruction previously
in memory. */
typedef struct bkptrec
{
mem_addr addr;
instruction *inst;
struct bkptrec *next;
} bkpt;
static bkpt *bkpts = NULL;
/* Set a breakpoint at memory location ADDR. */
void
add_breakpoint (mem_addr addr)
{
bkpt *rec = (bkpt *) xmalloc (sizeof (bkpt));
rec->next = bkpts;
rec->addr = addr;
if ((rec->inst = set_breakpoint (addr)) != NULL)
bkpts = rec;
else
{
if (exception_occurred)
error ("Cannot put a breakpoint at address 0x%08x\n", addr);
else
error ("No instruction to breakpoint at address 0x%08x\n", addr);
free (rec);
}
}
/* Delete all breakpoints at memory location ADDR. */
void
delete_breakpoint (mem_addr addr)
{
bkpt *p, *b;
int deleted_one = 0;
for (p = NULL, b = bkpts; b != NULL; )
if (b->addr == addr)
{
bkpt *n;
set_mem_inst (addr, b->inst);
if (p == NULL)
bkpts = b->next;
else
p->next = b->next;
n = b->next;
free (b);
b = n;
deleted_one = 1;
}
else
p = b, b = b->next;
if (!deleted_one)
error ("No breakpoint to delete at 0x%08x\n", addr);
}
static void
delete_all_breakpoints ()
{
bkpt *b, *n;
for (b = bkpts, n = NULL; b != NULL; b = n)
{
n = b->next;
free (b);
}
bkpts = NULL;
}
/* List all breakpoints. */
void
list_breakpoints ()
{
bkpt *b;
if (bkpts)
for (b = bkpts; b != NULL; b = b->next)
write_output (message_out, "Breakpoint at 0x%08x\n", b->addr);
else
write_output (message_out, "No breakpoints set\n");
}
/* Utility routines */
/* Return the entry in the linear TABLE of length LENGTH with key STRING.
TABLE must be sorted on the key field.
Return NULL if no such entry exists. */
name_val_val *
map_string_to_name_val_val (name_val_val tbl[], int tbl_len, char *id)
{
int low = 0;
int hi = tbl_len - 1;
while (low <= hi)
{
int mid = (low + hi) / 2;
char *idp = id, *np = tbl[mid].name;
while (*idp == *np && *idp != '\0') {idp ++; np ++;}
if (*np == '\0' && *idp == '\0') /* End of both strings */
return (& tbl[mid]);
else if (*idp > *np)
low = mid + 1;
else
hi = mid - 1;
}
return NULL;
}
/* Return the entry in the linear TABLE of length LENGTH with VALUE1 field NUM.
TABLE must be sorted on the VALUE1 field.
Return NULL if no such entry exists. */
name_val_val *
map_int_to_name_val_val (name_val_val tbl[], int tbl_len, int num)
{
int low = 0;
int hi = tbl_len - 1;
while (low <= hi)
{
int mid = (low + hi) / 2;
if (tbl[mid].value1 == num)
return (&tbl[mid]);
else if (num > tbl[mid].value1)
low = mid + 1;
else
hi = mid - 1;
}
return NULL;
}
#ifdef NEED_VSPRINTF
char *
vsprintf (str, fmt, args)
char *str,*fmt;
va_list *args;
{
FILE _strbuf;
_strbuf._flag = _IOWRT+_IOSTRG;
_strbuf._ptr = str;
_strbuf._cnt = 32767;
_doprnt(fmt, args, &_strbuf);
putc('\0', &_strbuf);
return(str);
}
#endif
#ifdef NEED_STRTOL
unsigned long
strtol (const char* str, const char** eptr, int base)
{
long result;
if (base != 0 && base != 16)
fatal_error ("SPIM's strtol only works for base 16 (not base %d)\n", base);
if (*str == '0' && (*(str + 1) == 'x' || *(str + 1) == 'X'))
{
str += 2;
sscanf (str, "%lx", &result);
}
else if (base == 16)
{
sscanf (str, "%lx", &result);
}
else
{
sscanf (str, "%ld", &result);
}
return (result);
}
#endif
#ifdef NEED_STRTOUL
unsigned long
strtoul (const char* str, char** eptr, int base)
{
unsigned long result;
if (base != 0 && base != 16)
fatal_error ("SPIM's strtoul only works for base 16 (not base %d)\n", base);
if (*str == '0' && (*(str + 1) == 'x' || *(str + 1) == 'X'))
{
str += 2;
sscanf (str, "%lx", &result);
}
else if (base == 16)
{
sscanf (str, "%lx", &result);
}
else
{
sscanf (str, "%ld", &result);
}
return (result);
}
#endif
char *
str_copy (char *str)
{
return (strcpy (xmalloc (strlen (str) + 1), str));
}
void *
xmalloc (int size)
{
void *x = (void *) malloc (size);
if (x == 0)
fatal_error ("Out of memory at request for %d bytes.\n");
return (x);
}
/* Allocate a zero'ed block of storage. */
void *
zmalloc (int size)
{
void *z = (void *) malloc (size);
if (z == 0)
fatal_error ("Out of memory at request for %d bytes.\n");
memclr (z, size);
return (z);
}