Blame view

sim2c-1.0/test3.c 3.16 KB
f2c4dc81   Salvador Abreu   added most of the...
1
#include <stdio.h>
fb3afe45   Salvador Abreu   doing 64bits now...
2

f2c4dc81   Salvador Abreu   added most of the...
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// == Global data =============================================================

#define W  * 4
#define KB * 1024
#define MB KB KB

#define DATA_SZ   1 MB
#define HEAP_SZ   1 MB
#define STACK_SZ  1 MB

#define MEM_SZ ((DATA_SZ) + (HEAP_SZ) + (STACK_SZ))

union {
  int mem[0];
fb3afe45   Salvador Abreu   doing 64bits now...
17
  struct {
f2c4dc81   Salvador Abreu   added most of the...
18
    int m_x;
fb3afe45   Salvador Abreu   doing 64bits now...
19
20
    int m_xpto[20];
  } named;
f2c4dc81   Salvador Abreu   added most of the...
21
22
  struct {
    int z_data[DATA_SZ];
fb3afe45   Salvador Abreu   doing 64bits now...
23
24
25
    int z_heap[HEAP_SZ];
    int z_stack[STACK_SZ];
  } zone;
f2c4dc81   Salvador Abreu   added most of the...
26
27
28
29
30
31
32
33
} global;

#define M      global.mem
#define DATA   global.zone.z_data
#define HEAP   global.zone.z_heap
#define STACK  global.zone.z_stack

int SP = MEM_SZ;
fb3afe45   Salvador Abreu   doing 64bits now...
34
35
36
int FP = 0;
int SR;
void *PC;				// (unused)
f2c4dc81   Salvador Abreu   added most of the...
37
38
39
40

// -- Names for global variables ----------------------------------------------

#define GV_x ((int *) &global.named.m_x - (int *) &global.named)
fb3afe45   Salvador Abreu   doing 64bits now...
41
42
#define GV_xpto ((int *) &global.named.m_xpto - (int *) &global.named)

f2c4dc81   Salvador Abreu   added most of the...
43
44
45
46
47
// == Program =================================================================

int main (int argc, char *argv[])
{
  int i;
fb3afe45   Salvador Abreu   doing 64bits now...
48

f2c4dc81   Salvador Abreu   added most of the...
49
50
51
52
53
54
55
56
57
58
// -- Initialization ----------------------------------------------------------
  global.named.m_x = 123;
  for (i=0; i<20; ++i) global.named.m_xpto[i] = 0;

// -- Library -----------------------------------------------------------------

	goto lib_init;			// skip library code!

print_int:				// print_int (int) -> ()
	printf ("%d\n", M[SP+1]);
fb3afe45   Salvador Abreu   doing 64bits now...
59
	goto * ((void *) M[SP++]);
f2c4dc81   Salvador Abreu   added most of the...
60
61
62
63
64
65
66

print_char:				// print_char (int) -> ()
	putchar (M[SP+1]);
	goto * ((void *) M[SP++]);

read_int:				// read_int () -> int
	scanf ("%d", &M[SP+1]);
fb3afe45   Salvador Abreu   doing 64bits now...
67
	goto * ((void *) M[SP++]);
f2c4dc81   Salvador Abreu   added most of the...
68
69
70
71
72
73
74
75
76
77
78
79

read_char:				// read_char () -> int
	M[SP+1] = getchar ();
	goto * ((void *) M[SP++]);

halt:					// halt () -> ()
	return 0;

dump_regs:				// dump_regs () -> ()
	{
	  printf ("-- Register dump --\n");
	  printf ("SP = 0x%x (%d)\n", (int) SP, (int) SP);
fb3afe45   Salvador Abreu   doing 64bits now...
80
81
82
	  printf ("FP = 0x%x (%d)\n", (int) FP, (int) FP);
	  printf ("SR = 0x%x (%d)\n", (int) SR, (int) SR);
	  printf ("PC = (unused)\n");
f2c4dc81   Salvador Abreu   added most of the...
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
	}
	goto * ((void *) M[SP++]);

dump_stack:				// dump_stack () -> ()
	{
	  printf ("-- Stack dump --\n");
	}
	goto * ((void *) M[SP++]);

stack_trace:				// stack_trace () -> ()
	{
	  printf ("-- Stack trace --\n");
	}
	goto * ((void *) M[SP++]);

lib_init:
	{
	  // (declarations for function ) (print_int);
	  // (declarations for function ) (print_char);
	  // (declarations for function ) (read_int);
	  // (declarations for function ) (read_char);
	  // (declarations for function ) (halt);
	  // (declarations for function ) (dump_regs);
	  // (declarations for function ) (dump_stack);
	  // (declarations for function ) (stack_trace);
	}

// -- Start execution ---------------------------------------------------------
  M[--SP] = (int) &&L_exit_program; // Save return address for main program
fb3afe45   Salvador Abreu   doing 64bits now...
112
  goto program;			// start kicking...
f2c4dc81   Salvador Abreu   added most of the...
113
114
115
116
117
118
119
120
L_exit_program:			// Return here, and...
  exit (0);			// quit.
  

// -- Instructions ------------------------------------------------------------
program:
	M[--SP] = 0;	// PUSH 0
	{ int N = M[SP]; M[SP]= FP; FP=SP+1; SP -= N; } // LINK
fb3afe45   Salvador Abreu   doing 64bits now...
121
	M[--SP] = 12;	// PUSH 12
f2c4dc81   Salvador Abreu   added most of the...
122
	M[--SP] = (int) &&print_int;	// PUSH print_int
fb3afe45   Salvador Abreu   doing 64bits now...
123
124
	{ void *C = (void *) M[SP]; M[SP]=(int)&&P_6; goto *C; } // CALL
P_6:	SP=FP; FP=M[SP-1];		// UNLINK
f2c4dc81   Salvador Abreu   added most of the...
125
126
127
	goto * ((void *) M[SP++]);		// JUMP

}