-
Notifications
You must be signed in to change notification settings - Fork 0
/
CelciusFarenheit.power.c
403 lines (339 loc) · 14 KB
/
CelciusFarenheit.power.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
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
/* Begin Header autogenerated part */
#include "h2-power-power.h"
#define power_G32(INSN){ *(h2_asm_pc++) = (INSN);}
void P1_BLR__I_32(void){ /* ret */
#ifdef H2_DEBUG
printf("%p : P1_BLR__I_32\n", h2_asm_pc);
#endif
power_G32(((0x4e800020 >> 0) & 0xffffffff)); \
}
void P1_MULLW_RRR_I_32(int r0, int r1, int r2){ /* mul */
#ifdef H2_DEBUG
printf("%p : P1_MULLW_RRR_I_32\n", h2_asm_pc);
#endif
power_G32(((0x1f & 0x3f) << 26)|((r0 & 0x1f) << 21)|((r1 & 0x1f) << 16)|((r2 & 0x1f) << 11)|((0x1d6 & 0x7ff) >> 0)); \
}
void P1_MULLI_RRI_I_64(int r0, int r1, int i0){ /* mul */
#ifdef H2_DEBUG
printf("%p : P1_MULLI_RRI_I_64\n", h2_asm_pc);
#endif
power_G32(((0x7 & 0x3f) << 26)|((r0 & 0x1f) << 21)|((r1 & 0x1f) << 16)|((i0 & 0xffff) >> 0)); \
}
void P1_SUBI_RRI_I_32(int r0, int r1, int i0){ /* sub */
#ifdef H2_DEBUG
printf("%p : P1_SUBI_RRI_I_32\n", h2_asm_pc);
#endif
power_G32(((0xe & 0x3f) << 26)|((r0 & 0x1f) << 21)|((r1 & 0x1f) << 16)|((-i0 & 0xffff) >> 0)); \
}
void PPC_SUB_RRR_I_32(int r0, int r2, int r1){ /* sub */
#ifdef H2_DEBUG
printf("%p : PPC_SUB_RRR_I_32\n", h2_asm_pc);
#endif
power_G32(((0x1f & 0x3f) << 26)|((r0 & 0x1f) << 21)|((r1 & 0x1f) << 16)|((r2 & 0x1f) << 11)|((0x50 & 0x7ff) >> 0)); \
}
void V2_03_VADDSBS_RRR_I_8(int r0, int r1, int r2){ /* add */
#ifdef H2_DEBUG
printf("%p : V2_03_VADDSBS_RRR_I_8\n", h2_asm_pc);
#endif
power_G32(((0x4 & 0x3f) << 26)|((r0 & 0x1f) << 21)|((r1 & 0x1f) << 16)|((r2 & 0x1f) << 11)|((0x300 & 0x7ff) >> 0)); \
}
void V2_03_VADDUBS_RRR_I_8(int r0, int r1, int r2){ /* add */
#ifdef H2_DEBUG
printf("%p : V2_03_VADDUBS_RRR_I_8\n", h2_asm_pc);
#endif
power_G32(((0x4 & 0x3f) << 26)|((r0 & 0x1f) << 21)|((r1 & 0x1f) << 16)|((r2 & 0x1f) << 11)|((0x200 & 0x7ff) >> 0)); \
}
void V2_03_VADDUHS_RRR_I_16(int r0, int r1, int r2){ /* add */
#ifdef H2_DEBUG
printf("%p : V2_03_VADDUHS_RRR_I_16\n", h2_asm_pc);
#endif
power_G32(((0x4 & 0x3f) << 26)|((r0 & 0x1f) << 21)|((r1 & 0x1f) << 16)|((r2 & 0x1f) << 11)|((0x240 & 0x7ff) >> 0)); \
}
void V2_03_VADDSHS_RRR_I_16(int r0, int r1, int r2){ /* add */
#ifdef H2_DEBUG
printf("%p : V2_03_VADDSHS_RRR_I_16\n", h2_asm_pc);
#endif
power_G32(((0x4 & 0x3f) << 26)|((r0 & 0x1f) << 21)|((r1 & 0x1f) << 16)|((r2 & 0x1f) << 11)|((0x340 & 0x7ff) >> 0)); \
}
void V2_03_VADDUWS_RRR_I_32(int r0, int r1, int r2){ /* add */
#ifdef H2_DEBUG
printf("%p : V2_03_VADDUWS_RRR_I_32\n", h2_asm_pc);
#endif
power_G32(((0x4 & 0x3f) << 26)|((r0 & 0x1f) << 21)|((r1 & 0x1f) << 16)|((r2 & 0x1f) << 11)|((0x280 & 0x7ff) >> 0)); \
}
void P1_ADDI_RRI_I_32(int r0, int r1, int i0){ /* add */
#ifdef H2_DEBUG
printf("%p : P1_ADDI_RRI_I_32\n", h2_asm_pc);
#endif
power_G32(((0xe & 0x3f) << 26)|((r0 & 0x1f) << 21)|((r1 & 0x1f) << 16)|((i0 & 0xffff) >> 0)); \
}
void V2_03_VADDSWS_RRR_I_32(int r0, int r1, int r2){ /* add */
#ifdef H2_DEBUG
printf("%p : V2_03_VADDSWS_RRR_I_32\n", h2_asm_pc);
#endif
power_G32(((0x4 & 0x3f) << 26)|((r0 & 0x1f) << 21)|((r1 & 0x1f) << 16)|((r2 & 0x1f) << 11)|((0x380 & 0x7ff) >> 0)); \
}
void P1_ADD_RRR_I_32(int r0, int r1, int r2){ /* add */
#ifdef H2_DEBUG
printf("%p : P1_ADD_RRR_I_32\n", h2_asm_pc);
#endif
power_G32(((0x1f & 0x3f) << 26)|((r0 & 0x1f) << 21)|((r1 & 0x1f) << 16)|((r2 & 0x1f) << 11)|((0x214 & 0x7ff) >> 0)); \
}
void P1_LI_RI_I_32(int r0, int i0){ /* mv */
#ifdef H2_DEBUG
printf("%p : P1_LI_RI_I_32\n", h2_asm_pc);
#endif
power_G32(((0xe & 0x3f) << 26)|((r0 & 0x1f) << 21)|((0x0 & 0x1f) << 16)|((i0 & 0xffff) >> 0)); \
}
void P1_MV_RR_I_32(int r0, int r1){ /* mv */
#ifdef H2_DEBUG
printf("%p : P1_MV_RR_I_32\n", h2_asm_pc);
#endif
power_G32(((0xe & 0x3f) << 26)|((r0 & 0x1f) << 21)|((r1 & 0x1f) << 16)|((0x0 & 0xffff) >> 0)); \
}
void PPC_DIVW_RRR_I_32(int r0, int r1, int r2){ /* div */
#ifdef H2_DEBUG
printf("%p : PPC_DIVW_RRR_I_32\n", h2_asm_pc);
#endif
power_G32(((0x1f & 0x3f) << 26)|((r0 & 0x1f) << 21)|((r1 & 0x1f) << 16)|((r2 & 0x1f) << 11)|((0x3d6 & 0x7ff) >> 0)); \
}
void PPC_DIVD_RRR_I_64(int r0, int r1, int r2){ /* div */
#ifdef H2_DEBUG
printf("%p : PPC_DIVD_RRR_I_64\n", h2_asm_pc);
#endif
power_G32(((0x1f & 0x3f) << 26)|((r0 & 0x1f) << 21)|((r1 & 0x1f) << 16)|((r2 & 0x1f) << 11)|((0x3d2 & 0x7ff) >> 0)); \
}
void PPC_DIVD__RRR_I_64(int r0, int r1, int r2){ /* div */
#ifdef H2_DEBUG
printf("%p : PPC_DIVD__RRR_I_64\n", h2_asm_pc);
#endif
power_G32(((0x1f & 0x3f) << 26)|((r0 & 0x1f) << 21)|((r1 & 0x1f) << 16)|((r2 & 0x1f) << 11)|((0x3d3 & 0x7ff) >> 0)); \
}
void PPC_DIVDO_RRR_I_64(int r0, int r1, int r2){ /* div */
#ifdef H2_DEBUG
printf("%p : PPC_DIVDO_RRR_I_64\n", h2_asm_pc);
#endif
power_G32(((0x1f & 0x3f) << 26)|((r0 & 0x1f) << 21)|((r1 & 0x1f) << 16)|((r2 & 0x1f) << 11)|((0x7d2 & 0x7ff) >> 0)); \
}
void PPC_DIVDO__RRR_I_64(int r0, int r1, int r2){ /* div */
#ifdef H2_DEBUG
printf("%p : PPC_DIVDO__RRR_I_64\n", h2_asm_pc);
#endif
power_G32(((0x1f & 0x3f) << 26)|((r0 & 0x1f) << 21)|((r1 & 0x1f) << 16)|((r2 & 0x1f) << 11)|((0x7d3 & 0x7ff) >> 0)); \
}
void power_genRET_0()
{
P1_BLR__I_32();
}
void power_genSUB_3(h2_sValue_t P0, h2_sValue_t P1, h2_sValue_t P2)
{
if ((P0.arith == 'i') && (P0.wLen <= 32) && (P0.vLen == 1) && P0.ValOrReg == REGISTER && P1.ValOrReg == REGISTER && P2.ValOrReg == VALUE)
{
P1_SUBI_RRI_I_32(P0.regNro, P1.regNro, P2.valueImm);
}
else if ((P0.arith == 'i') && (P0.wLen <= 32) && (P0.vLen == 1) && P0.ValOrReg == REGISTER && P1.ValOrReg == REGISTER && P2.ValOrReg == REGISTER)
{
PPC_SUB_RRR_I_32(P0.regNro, P1.regNro, P2.regNro);
}
else
{
printf("Warning, generation of SUB is not possible with this arguments P0: %c, %i, %i, P0: %i , P1: %i , P2: %i \n", P0.arith, P0.wLen, P0.vLen, P0.ValOrReg ,P1.ValOrReg ,P2.ValOrReg);
h2_codeGenerationOK = 0;
}
}
void power_genMUL_3(h2_sValue_t P0, h2_sValue_t P1, h2_sValue_t P2)
{
if ((P0.arith == 'i') && (P0.wLen <= 32) && (P0.vLen == 1) && P0.ValOrReg == REGISTER && P1.ValOrReg == REGISTER && P2.ValOrReg == REGISTER)
{
P1_MULLW_RRR_I_32(P0.regNro, P1.regNro, P2.regNro);
}
else if ((P0.arith == 'i') && (P0.wLen <= 64) && (P0.vLen == 1) && P0.ValOrReg == REGISTER && P1.ValOrReg == REGISTER && P2.ValOrReg == VALUE)
{
P1_MULLI_RRI_I_64(P0.regNro, P1.regNro, P2.valueImm);
}
else
{
printf("Warning, generation of MUL is not possible with this arguments P0: %c, %i, %i, P0: %i , P1: %i , P2: %i \n", P0.arith, P0.wLen, P0.vLen, P0.ValOrReg ,P1.ValOrReg ,P2.ValOrReg);
h2_codeGenerationOK = 0;
}
}
void power_genADD_3(h2_sValue_t P0, h2_sValue_t P1, h2_sValue_t P2)
{
if ((P0.arith == 'i') && (P0.wLen <= 8) && (P0.vLen == 16) && P0.ValOrReg == REGISTER && P1.ValOrReg == REGISTER && P2.ValOrReg == REGISTER)
{
V2_03_VADDSBS_RRR_I_8(P0.regNro, P1.regNro, P2.regNro);
}
else if ((P0.arith == 'i') && (P0.wLen <= 8) && (P0.vLen == 16) && P0.ValOrReg == REGISTER && P1.ValOrReg == REGISTER && P2.ValOrReg == REGISTER)
{
V2_03_VADDUBS_RRR_I_8(P0.regNro, P1.regNro, P2.regNro);
}
else if ((P0.arith == 'i') && (P0.wLen <= 16) && (P0.vLen == 8) && P0.ValOrReg == REGISTER && P1.ValOrReg == REGISTER && P2.ValOrReg == REGISTER)
{
V2_03_VADDUHS_RRR_I_16(P0.regNro, P1.regNro, P2.regNro);
}
else if ((P0.arith == 'i') && (P0.wLen <= 16) && (P0.vLen == 8) && P0.ValOrReg == REGISTER && P1.ValOrReg == REGISTER && P2.ValOrReg == REGISTER)
{
V2_03_VADDSHS_RRR_I_16(P0.regNro, P1.regNro, P2.regNro);
}
else if ((P0.arith == 'i') && (P0.wLen <= 32) && (P0.vLen == 4) && P0.ValOrReg == REGISTER && P1.ValOrReg == REGISTER && P2.ValOrReg == REGISTER)
{
V2_03_VADDUWS_RRR_I_32(P0.regNro, P1.regNro, P2.regNro);
}
else if ((P0.arith == 'i') && (P0.wLen <= 32) && (P0.vLen == 1) && P0.ValOrReg == REGISTER && P1.ValOrReg == REGISTER && P2.ValOrReg == VALUE)
{
P1_ADDI_RRI_I_32(P0.regNro, P1.regNro, P2.valueImm);
}
else if ((P0.arith == 'i') && (P0.wLen <= 32) && (P0.vLen == 4) && P0.ValOrReg == REGISTER && P1.ValOrReg == REGISTER && P2.ValOrReg == REGISTER)
{
V2_03_VADDSWS_RRR_I_32(P0.regNro, P1.regNro, P2.regNro);
}
else if ((P0.arith == 'i') && (P0.wLen <= 32) && (P0.vLen == 1) && P0.ValOrReg == REGISTER && P1.ValOrReg == REGISTER && P2.ValOrReg == REGISTER)
{
P1_ADD_RRR_I_32(P0.regNro, P1.regNro, P2.regNro);
}
else
{
printf("Warning, generation of ADD is not possible with this arguments P0: %c, %i, %i, P0: %i , P1: %i , P2: %i \n", P0.arith, P0.wLen, P0.vLen, P0.ValOrReg ,P1.ValOrReg ,P2.ValOrReg);
h2_codeGenerationOK = 0;
}
}
void power_genMV_2(h2_sValue_t P0, h2_sValue_t P1)
{
if ((P0.arith == 'i') && (P0.wLen <= 32) && (P0.vLen == 1) && P0.ValOrReg == REGISTER && P1.ValOrReg == VALUE)
{
P1_LI_RI_I_32(P0.regNro, P1.valueImm);
}
else if ((P0.arith == 'i') && (P0.wLen <= 32) && (P0.vLen == 1) && P0.ValOrReg == REGISTER && P1.ValOrReg == REGISTER)
{
P1_MV_RR_I_32(P0.regNro, P1.regNro);
}
else
{
printf("Warning, generation of MV is not possible with this arguments P0: %c, %i, %i, P0: %i , P1: %i \n", P0.arith, P0.wLen, P0.vLen, P0.ValOrReg ,P1.ValOrReg);
h2_codeGenerationOK = 0;
}
}
void power_genDIV_3(h2_sValue_t P0, h2_sValue_t P1, h2_sValue_t P2)
{
if ((P0.arith == 'i') && (P0.wLen <= 32) && (P0.vLen == 1) && P0.ValOrReg == REGISTER && P1.ValOrReg == REGISTER && P2.ValOrReg == REGISTER)
{
PPC_DIVW_RRR_I_32(P0.regNro, P1.regNro, P2.regNro);
}
else if ((P0.arith == 'i') && (P0.wLen <= 64) && (P0.vLen == 1) && P0.ValOrReg == REGISTER && P1.ValOrReg == REGISTER && P2.ValOrReg == REGISTER)
{
PPC_DIVD_RRR_I_64(P0.regNro, P1.regNro, P2.regNro);
}
else if ((P0.arith == 'i') && (P0.wLen <= 64) && (P0.vLen == 1) && P0.ValOrReg == REGISTER && P1.ValOrReg == REGISTER && P2.ValOrReg == REGISTER)
{
PPC_DIVD__RRR_I_64(P0.regNro, P1.regNro, P2.regNro);
}
else if ((P0.arith == 'i') && (P0.wLen <= 64) && (P0.vLen == 1) && P0.ValOrReg == REGISTER && P1.ValOrReg == REGISTER && P2.ValOrReg == REGISTER)
{
PPC_DIVDO_RRR_I_64(P0.regNro, P1.regNro, P2.regNro);
}
else if ((P0.arith == 'i') && (P0.wLen <= 64) && (P0.vLen == 1) && P0.ValOrReg == REGISTER && P1.ValOrReg == REGISTER && P2.ValOrReg == REGISTER)
{
PPC_DIVDO__RRR_I_64(P0.regNro, P1.regNro, P2.regNro);
}
else
{
printf("Warning, generation of DIV is not possible with this arguments P0: %c, %i, %i, P0: %i , P1: %i , P2: %i \n", P0.arith, P0.wLen, P0.vLen, P0.ValOrReg ,P1.ValOrReg ,P2.ValOrReg);
h2_codeGenerationOK = 0;
}
}
/* End Header autogenerated part */
// -*- c -*-
#include <stdio.h>
#include <stdlib.h>
typedef int (*pifi)(int);
h2_insn_t * genC2F(h2_insn_t * ptr)
{
/* Code Generation of 3 instructions */
/* Symbol table :*/
/*VarName = { ValOrLen, arith, vectorLen, wordLen, regNo, Value} */
h2_sValue_t a = {REGISTER, 'i', 1, 32, 3, 0};
h2_sValue_t h2_outputVarName = {REGISTER, 'i', 1, 32, 3, 0};
h2_sValue_t r = {REGISTER, 'i', 1, 32, 14, 0};
h2_sValue_t h2_00000000 = {REGISTER, 'i', 1, 32, 15, 0};
h2_sValue_t h2_00000001 = {REGISTER, 'i', 1, 32, 16, 0};
/* Label table :*/
#define power_genLABEL(LABEL_ID) labelAddresses[LABEL_ID] = h2_asm_pc;
h2_insn_t * labelAddresses []={
};
h2_asm_pc = (h2_insn_t *) ptr;
h2_codeGenerationOK = 1;
power_genMV_2(h2_00000000, (h2_sValue_t) {VALUE, 'i', 1, 32, 0, 9});
power_genMUL_3(h2_00000000, a, h2_00000000);
power_genMV_2(h2_00000001, (h2_sValue_t) {VALUE, 'i', 1, 32, 0, 5});
power_genDIV_3(h2_00000000, h2_00000000, h2_00000001);
power_genMV_2(h2_00000001, (h2_sValue_t) {VALUE, 'i', 1, 32, 0, 32});
power_genADD_3(h2_00000000, h2_00000000, h2_00000001);
power_genMV_2(r, h2_00000000);
power_genMV_2(h2_outputVarName, r);
power_genRET_0();
/* Call back code for loops */
h2_save_asm_pc = h2_asm_pc;
h2_asm_pc = h2_save_asm_pc;
iflush(ptr, h2_asm_pc);
return (h2_insn_t *) ptr;
}
h2_insn_t * genF2C(h2_insn_t * ptr)
{
/* Code Generation of 3 instructions */
/* Symbol table :*/
/*VarName = { ValOrLen, arith, vectorLen, wordLen, regNo, Value} */
h2_sValue_t a = {REGISTER, 'i', 1, 32, 3, 0};
h2_sValue_t h2_outputVarName = {REGISTER, 'i', 1, 32, 3, 0};
h2_sValue_t r = {REGISTER, 'i', 1, 32, 14, 0};
h2_sValue_t h2_00000000 = {REGISTER, 'i', 1, 32, 15, 0};
h2_sValue_t h2_00000001 = {REGISTER, 'i', 1, 32, 16, 0};
/* Label table :*/
#define power_genLABEL(LABEL_ID) labelAddresses[LABEL_ID] = h2_asm_pc;
h2_insn_t * labelAddresses []={
};
h2_asm_pc = (h2_insn_t *) ptr;
h2_codeGenerationOK = 1;
power_genMV_2(h2_00000000, (h2_sValue_t) {VALUE, 'i', 1, 32, 0, 160});
power_genMV_2(h2_00000001, (h2_sValue_t) {VALUE, 'i', 1, 32, 0, 9});
power_genDIV_3(h2_00000000, h2_00000000, h2_00000001);
power_genSUB_3(h2_00000000, a, h2_00000000);
power_genMV_2(r, h2_00000000);
power_genMV_2(h2_outputVarName, r);
power_genRET_0();
/* Call back code for loops */
h2_save_asm_pc = h2_asm_pc;
h2_asm_pc = h2_save_asm_pc;
iflush(ptr, h2_asm_pc);
return (h2_insn_t *) ptr;
}
int main(int argc, char * argv[])
{
h2_insn_t * ptr;
int in0, res, resultOK;
pifi fC2F, fF2C;
resultOK = 0;
if (argc < 2)
{
printf("Give 1 values\n");
exit(-1);
}
in0 = atoi (argv[1]); // Get the users values in0
printf("// Compilette for conversion from celcius to farenheit\n");
ptr = h2_malloc (1024); // Allocate memory for 1024 instructions
fC2F = (pifi) genC2F (ptr); //
res = fC2F(in0); // Call generated code
printf("%d (celcius) -> (farenheit)%d\n", in0, res);
if (res != (in0 * 9 / 5 + 32))
resultOK += 1;
printf("// Compilette for conversion from farenheit to celcius\n");
ptr = h2_malloc (1024); // Allocate memory for 1024 instructions
fF2C = (pifi) genF2C (ptr); //
res = fF2C(in0); // Call generated code
printf("%d (farenheit) -> (celcius)%d\n", in0, res);
if (res != (in0 - 32 * 5 / 9))
resultOK += 1;
if (resultOK == 0)
return 0;
else
return -1;
}