Blame view

sim2c-1.0/test3.c 3.21 KB
f2c4dc81   Salvador Abreu   added most of the...
1
#include <stdio.h>
fb3afe45   Salvador Abreu   doing 64bits now...
2
#include <stdlib.h>
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 {
fb3afe45   Salvador Abreu   doing 64bits now...
17
  long mem[0];
f2c4dc81   Salvador Abreu   added most of the...
18
  struct {
fb3afe45   Salvador Abreu   doing 64bits now...
19
20
    long m_x;
    long m_xpto[20];
f2c4dc81   Salvador Abreu   added most of the...
21
22
  } named;
  struct {
fb3afe45   Salvador Abreu   doing 64bits now...
23
24
25
    long z_data[DATA_SZ];
    long z_heap[HEAP_SZ];
    long z_stack[STACK_SZ];
f2c4dc81   Salvador Abreu   added most of the...
26
27
28
29
30
31
32
33
  } zone;
} global;

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

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

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

fb3afe45   Salvador Abreu   doing 64bits now...
41
42
#define GV_x ((long *) &global.named.m_x - (long *) &global.named)
#define GV_xpto ((long *) &global.named.m_xpto - (long *) &global.named)
f2c4dc81   Salvador Abreu   added most of the...
43
44
45
46
47

// == Program =================================================================

int main (int argc, char *argv[])
{
fb3afe45   Salvador Abreu   doing 64bits now...
48
  long i;
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) -> ()
fb3afe45   Salvador Abreu   doing 64bits now...
59
	printf ("%ld\n", M[SP+1]);
f2c4dc81   Salvador Abreu   added most of the...
60
61
62
63
64
65
66
	goto * ((void *) M[SP++]);

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

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

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");
fb3afe45   Salvador Abreu   doing 64bits now...
80
81
82
	  printf ("SP = 0x%lx (%ld)\n", (long) SP, (long) SP);
	  printf ("FP = 0x%lx (%ld)\n", (long) FP, (long) FP);
	  printf ("SR = 0x%lx (%ld)\n", (long) SR, (long) SR);
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
	  printf ("PC = (unused)\n");
	}
	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 ---------------------------------------------------------
fb3afe45   Salvador Abreu   doing 64bits now...
112
  M[--SP] = (long) &&L_exit_program; // Save return address for main program
f2c4dc81   Salvador Abreu   added most of the...
113
114
115
116
117
118
119
120
  goto program;			// start kicking...
L_exit_program:			// Return here, and...
  exit (0);			// quit.
  

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

}