summaryrefslogtreecommitdiff
path: root/tools/spasm/codegen.c
blob: fda456d14a17cda1e35b034d26790a1fcd66165e (plain) (blame)
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
#include "spasm.h"

char curIns[128];
unsigned int curInsArg;
unsigned int curInsArgT;
unsigned int insArgv[64];
unsigned int insArgc;
unsigned int insArgt[64];
unsigned int copn;
int org_found;

void (*INSFUNC)(void);

volatile unsigned int curPc = 0;
int curPass = 0;
unsigned int startAddress = 0;
unsigned int numLabels;
unsigned int numLabelsAlloc;
int first_instruction;
asm_label *labels;
static int find_label_status = 1;

void codegen_init(void)
{
	curPc = startAddress;
	curPass = 0;
	numLabels = 0;
	numLabelsAlloc = 0;
	labels = NULL;
}
		
static asm_label *find_label_internal(char *name)
{
	int i;
	
	for(i = 0; i < numLabels; i++)
	{
		if(strcmp(name, labels[i].name) == 0)
			return &labels[i];
	}
	
	return NULL;
}

static void add_label_internal(char *name, unsigned int pc)
{
	// add labels only if current pass >= 1!
	asm_label *l;
	
/*	if(curPass == )
		return;
	
	if(curPass >= 2)
		return;*/

	//printf("Name = %s\n", name);
	
	l = find_label_internal(name);	
		
	if(l)
	{
		if(l->pc != pc)
		{
			//if(l->pass == curPass)
			//	assembler_error("Impossible to redefine label %s", name);
			
			//printf("Redefining, [%s] = %08X, pass %d\n", l->name, pc, curPass);
			l->pc = pc;
		}
				
		return;
	}
	
	if(numLabels == numLabelsAlloc)
	{
		numLabelsAlloc += 128;
		labels = realloc(labels, sizeof(asm_label) * numLabelsAlloc);
	}
	
	strncpy(labels[numLabels].name, name, 127);
	labels[numLabels].pass = curPass;
	
	labels[numLabels].pc = pc;
		
	numLabels++;
	
	//printf("label #%d, [%s] = %08X, pass = %d\n", numLabels, name, pc, curPass);
	
	/*while(*name)
	{
		printf("%x, \'%c\'\n", *name, *name);
		name++;
	}*/
}

	

void add_label(char *name, unsigned int pc)
{
	if(curPass == -1)
		return;
	
	return add_label_internal(name, pc);
}

void add_label_equ(char *name, unsigned int pc)
{
	return add_label_internal(name, pc);
}

unsigned int find_label(char *name)
{
	//printf("find_label(%s)\n", name);
	
	asm_label *l = find_label_internal(name);
	
	if(l)
	{
		//find_label_status = 1;
		return l->pc;
	}
	
// remember! if pass >= 1, abort if you can't find a label, because that means it was really
// impossible to find.	
	
//	printf(">>DEBUG, PASS = %d << Couldn't find label %s$\n",  curPass, name);
		
	find_label_status = 0;
	
	if(curPass == 1)
		instruction_error("Cannot find label %s", name);
	
	return 0xFFFFFFFF;
}

void find_label_reset()
{
	find_label_status = 1;
}

int find_label_ok()
{
	return find_label_status;
}