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
|
//-----------------------------------------------------------------------------
// Copyright 2007 Jonathan Westhues
//
// This file is part of LDmicro.
//
// LDmicro is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// LDmicro is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with LDmicro. If not, see <http://www.gnu.org/licenses/>.
//------
//
// A crunched-down version of the intermediate code (e.g. assigning addresses
// to all the variables instead of just working with their names), suitable
// for interpretation.
// Jonathan Westhues, Aug 2005
//-----------------------------------------------------------------------------
#include "linuxUI.h"
#include <stdio.h>
#include <setjmp.h>
#include <stdlib.h>
#include "ldmicro.h"
#include "intcode.h"
static char Variables[MAX_IO][MAX_NAME_LEN];
static int VariablesCount;
static char InternalRelays[MAX_IO][MAX_NAME_LEN];
static int InternalRelaysCount;
typedef struct {
WORD op;
WORD name1;
WORD name2;
WORD name3;
SWORD literal;
} BinOp;
static BinOp OutProg[MAX_INT_OPS];
static WORD AddrForInternalRelay(char *name)
{
int i;
for(i = 0; i < InternalRelaysCount; i++) {
if(strcmp(InternalRelays[i], name)==0) {
return i;
}
}
strcpy(InternalRelays[i], name);
InternalRelaysCount++;
return i;
}
static WORD AddrForVariable(char *name)
{
int i;
for(i = 0; i < VariablesCount; i++) {
if(strcmp(Variables[i], name)==0) {
return i;
}
}
strcpy(Variables[i], name);
VariablesCount++;
return i;
}
static void Write(FILE *f, BinOp *op)
{
BYTE *b = (BYTE *)op;
int i;
for(i = 0; i < sizeof(*op); i++) {
fprintf(f, "%02x", b[i]);
}
fprintf(f, "\n");
}
void CompileInterpreted(char *outFile)
{
FILE *f = fopen(outFile, "w");
if(!f) {
Error(_("Couldn't write to '%s'"), outFile);
return;
}
InternalRelaysCount = 0;
VariablesCount = 0;
fprintf(f, "$$LDcode\n");
int ipc;
int outPc;
BinOp op;
// Convert the if/else structures in the intermediate code to absolute
// conditional jumps, to make life a bit easier for the interpreter.
#define MAX_IF_NESTING 32
int ifDepth = 0;
// PC for the if(...) instruction, which we will complete with the
// 'jump to if false' address (which is either the ELSE+1 or the ENDIF+1)
int ifOpIf[MAX_IF_NESTING];
// PC for the else instruction, which we will complete with the
// 'jump to if reached' address (which is the ENDIF+1)
int ifOpElse[MAX_IF_NESTING];
outPc = 0;
for(ipc = 0; ipc < IntCodeLen; ipc++) {
memset(&op, 0, sizeof(op));
op.op = IntCode[ipc].op;
switch(IntCode[ipc].op) {
case INT_CLEAR_BIT:
case INT_SET_BIT:
op.name1 = AddrForInternalRelay(IntCode[ipc].name1);
break;
case INT_COPY_BIT_TO_BIT:
op.name1 = AddrForInternalRelay(IntCode[ipc].name1);
op.name2 = AddrForInternalRelay(IntCode[ipc].name2);
break;
case INT_SET_VARIABLE_TO_LITERAL:
op.name1 = AddrForVariable(IntCode[ipc].name1);
op.literal = IntCode[ipc].literal;
break;
case INT_SET_VARIABLE_TO_VARIABLE:
op.name1 = AddrForVariable(IntCode[ipc].name1);
op.name2 = AddrForVariable(IntCode[ipc].name2);
break;
case INT_INCREMENT_VARIABLE:
op.name1 = AddrForVariable(IntCode[ipc].name1);
break;
case INT_SET_VARIABLE_ADD:
case INT_SET_VARIABLE_SUBTRACT:
case INT_SET_VARIABLE_MULTIPLY:
case INT_SET_VARIABLE_DIVIDE:
op.name1 = AddrForVariable(IntCode[ipc].name1);
op.name2 = AddrForVariable(IntCode[ipc].name2);
op.name3 = AddrForVariable(IntCode[ipc].name3);
break;
case INT_IF_BIT_SET:
case INT_IF_BIT_CLEAR:
op.name1 = AddrForInternalRelay(IntCode[ipc].name1);
goto finishIf;
case INT_IF_VARIABLE_LES_LITERAL:
op.name1 = AddrForVariable(IntCode[ipc].name1);
op.literal = IntCode[ipc].literal;
goto finishIf;
case INT_IF_VARIABLE_EQUALS_VARIABLE:
case INT_IF_VARIABLE_GRT_VARIABLE:
op.name1 = AddrForVariable(IntCode[ipc].name1);
op.name2 = AddrForVariable(IntCode[ipc].name2);
goto finishIf;
finishIf:
ifOpIf[ifDepth] = outPc;
ifOpElse[ifDepth] = 0;
ifDepth++;
// jump target will be filled in later
break;
case INT_ELSE:
ifOpElse[ifDepth-1] = outPc;
// jump target will be filled in later
break;
case INT_END_IF:
--ifDepth;
if(ifOpElse[ifDepth] == 0) {
// There is no else; if should jump straight to the
// instruction after this one if the condition is false.
OutProg[ifOpIf[ifDepth]].name3 = outPc-1;
} else {
// There is an else clause; if the if is false then jump
// just past the else, and if the else is reached then
// jump to the endif.
OutProg[ifOpIf[ifDepth]].name3 = ifOpElse[ifDepth];
OutProg[ifOpElse[ifDepth]].name3 = outPc-1;
}
// But don't generate an instruction for this.
continue;
case INT_SIMULATE_NODE_STATE:
case INT_COMMENT:
// Don't care; ignore, and don't generate an instruction.
continue;
case INT_EEPROM_BUSY_CHECK:
case INT_EEPROM_READ:
case INT_EEPROM_WRITE:
case INT_READ_ADC:
case INT_SET_PWM:
case INT_UART_SEND:
case INT_UART_RECV:
default:
Error(_("Unsupported op (anything ADC, PWM, UART, EEPROM) for "
"interpretable target."));
fclose(f);
return;
}
memcpy(&OutProg[outPc], &op, sizeof(op));
outPc++;
}
int i;
for(i = 0; i < outPc; i++) {
Write(f, &OutProg[i]);
}
memset(&op, 0, sizeof(op));
op.op = INT_END_OF_PROGRAM;
Write(f, &op);
fprintf(f, "$$bits\n");
for(i = 0; i < InternalRelaysCount; i++) {
if(InternalRelays[i][0] != '$') {
fprintf(f, "%s,%d\n", InternalRelays[i], i);
}
}
fprintf(f, "$$int16s\n");
for(i = 0; i < VariablesCount; i++) {
if(Variables[i][0] != '$') {
fprintf(f, "%s,%d\n", Variables[i], i);
}
}
fprintf(f, "$$cycle %d us\n", Prog.cycleTime);
fclose(f);
char str[MAX_PATH+500];
sprintf(str,
_("Compile successful; wrote interpretable code to '%s'.\r\n\r\n"
"You probably have to adapt the interpreter to your application. See "
"the documentation."), outFile);
CompileSuccessfulMessage(str);
}
|