-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathoptimise.c
117 lines (90 loc) · 2.52 KB
/
optimise.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
/*
isDcc
(c) 1998 Andrew de Quincey
See README.TXT for copying/distribution/modification details.
*/
#include "optimise.h"
static int instructionCounter;
static void optIf(ISData* isData, int line);
static void memzero(void* memstart, int length);
void optimise(ISData* isData)
{
int i;
for(i=0; i< isData->codeLinesCount; i++)
{
if (isData->codeLines[i] == NULL)
continue;
switch(isData->codeLines[i]->type)
{
case IFSTATEMENT:
// optIf(isData, i);
break;
}
}
printf("hi\n");
}
static void optIf(ISData* isData, int line)
{
CodeLine* curLine;
int ifCodeStart;
int nextCodeStart;
int elseCodeStart;
int numSubCodeLines;
int ifSize;
int elseSize;
int hasElse = 0;
curLine = isData->codeLines[line];
// ok, work out stuff for an IF without an ELSE
ifCodeStart = line+1;
nextCodeStart = isData->labels[curLine->destLabel]->position;
numSubCodeLines =1;
ifSize = nextCodeStart - ifCodeStart;
// now, check if it's got an ELSE clause, and fix if it does
if ((isData->codeLines[nextCodeStart-1]->type == GOTO) &&
(isData->codeLines[nextCodeStart-1]->destLabel >
curLine->destLabel))
{
elseCodeStart = nextCodeStart;
nextCodeStart =
isData->labels[isData->codeLines[nextCodeStart-1]->destLabel]->position;
hasElse = 1;
numSubCodeLines =2;
elseSize = nextCodeStart - elseCodeStart;
}
// make up memory & copy
curLine->subCodeLinesCount = numSubCodeLines;
curLine->subCodeLines =
(CodeLine**) mallocX(sizeof(CodeLine*) * numSubCodeLines);
// do if lines
curLine->subCodeLines[0] =
(CodeLine*) mallocX(sizeof(CodeLine) * ifSize);
memcpy(curLine->subCodeLines[0],
isData->codeLines[ifCodeStart],
ifSize * sizeof(CodeLine*));
memzero(curLine->subCodeLines[0],
ifSize * sizeof(CodeLine*));
// do else lines if they are there
if (hasElse)
{
curLine->subCodeLines[1] =
(CodeLine*) mallocX(sizeof(CodeLine) * elseSize);
memcpy(curLine->subCodeLines[1],
isData->codeLines[elseCodeStart],
elseSize * sizeof(CodeLine*));
memzero(curLine->subCodeLines[1],
elseSize * sizeof(CodeLine*));
}
/*
printf("%i\n", ifSize);
if (hasElse)
printf("%i\n", elseSize);
*/
}
static void memzero(void* memstart, int length)
{
int i;
char* poo = (char*) memstart;
for(i=0; i< length; i++)
*(poo + i) = 0;
}