OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [gen_or1k_isa/] [sources/] [gen_or1k_isa.c] - Blame information for rev 95

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 14 lampret
 
2
#include <stdio.h>
3 95 lampret
#include <stdlib.h>
4 14 lampret
 
5
#define NO_RELOC 20
6
#define RELOC_32 1
7
#define RELOC_8 2
8
#define RELOC_CONST 3
9
#define RELOC_CONSTH 4
10
 
11
#include "or1.h"
12
#include "or1k_isadesc.h"
13
 
14
/* types of data in encoding field. */
15
typedef enum { opcode, reserved, operand } encfld_types;
16
 
17
/* encoding transformed for TeX output */
18
struct {
19
        int bitpos[33];
20
        int fldcnt;
21
        int insn_size;
22
        struct {
23
                encfld_types type;
24
                int value;
25
                int bitsize;
26
        } field[50];
27
} decoded;
28
 
29 87 lampret
struct info {
30
        struct or1k_isa *desc;
31
        struct or32_opcode *opcode;
32
} info;
33
 
34
void transform_tex(char *input, char *output)
35
{
36
        while (*input != '\0') {
37
                if (*input == '[') {
38
                        *output++ = '{';
39
                        *output++ = '[';
40
                        *output++ = '}';
41
                } else if (*input == ']') {
42
                        *output++ = '{';
43
                        *output++ = ']';
44
                        *output++ = '}';
45
                } else if (*input == '%') {
46
                        *output++ = '\\';
47
                        *output++ = '%';
48
                } else if (*input == '\\') {
49
                        *output++ = '\\';
50
                        *output++ = '\\';
51
                } else if (*input == '_') {
52
                        *output++ = '\\';
53
                        *output++ = '_';
54
                } else if (*input == '\t') {
55
                        *output++ = '\\';
56
                        *output++ = 'i';
57
                        *output++ = 'n';
58
                        *output++ = 'd';
59
                        *output++ = 'e';
60
                        *output++ = 'n';
61
                        *output++ = 't';
62
                        *output++ = ' ';
63
                } else
64
                        *output++ = *input;
65
                input++;
66
        }
67
        *output = '\0';
68
}
69
 
70 95 lampret
void eliminate_crcr(char *input, char *output)
71
{
72
        while (*input != '\0') {
73
                if (*input == '\\') {
74
/*                      *output++ = '{';
75
                        *output++ = '[';*/
76
                } else
77
                        *output++ = *input;
78
                input++;
79
        }
80
        *output = '\0';
81
}
82
 
83 14 lampret
void print_encoding()
84
{
85
        int i, j;
86
 
87
        printf("\\vspace{10mm}\n");
88
        printf("{\\centering \\begin{tabular}{");
89
 
90
        /* organization of the table */
91
        for(i = 1; i <= decoded.fldcnt; i++) {
92
                printf("|");
93
                for(j = 0; j < decoded.field[i].bitsize; j++)
94 87 lampret
                        if (j == 0)
95
                                printf("l");
96
                        else if (j+1 == decoded.field[i].bitsize)
97
                                printf("r");
98
                        else
99
                                printf("c");
100 14 lampret
        }
101
        printf("|}\n\\hline\n");
102
 
103
        /* print first row of the table */
104
        for(i = decoded.insn_size - 1; i > 0; i--)
105
                if (decoded.bitpos[i])
106
                        printf("%d&\n", i);
107
                else
108 87 lampret
                        printf(".&\n");
109 14 lampret
        printf("0\\\\\n\\hline\n");
110
 
111
        /* print second row of the table */
112
        for(i = 1; i <= decoded.fldcnt; i++) {
113
                if (decoded.field[i].type == opcode) {
114
                        printf("\\multicolumn{%d}{", decoded.field[i].bitsize);
115
                        printf("%s", (i == 1 ? "|":""));
116
                        printf("c|}{opcode 0x%x}", decoded.field[i].value);
117
                } else if (decoded.field[i].type == operand) {
118
                        printf("\\multicolumn{%d}{", decoded.field[i].bitsize);
119
                        printf("%s", (i == 1 ? "|":""));
120
                        printf("c|}{%c}", decoded.field[i].value);
121
                } else if (decoded.field[i].type == reserved) {
122
                        printf("\\multicolumn{%d}{", decoded.field[i].bitsize);
123
                        printf("%s", (i == 1 ? "|":""));
124
                        printf("c|}{reserved}", decoded.field[i].value);
125
                }
126
                if (i == decoded.fldcnt)
127
                        printf("\\\\\n");
128
                else
129
                        printf("&\n");
130
        }
131
        printf("\n\\hline\n");
132
 
133
        /* print third row of the table */
134
        for(i = 1; i < decoded.fldcnt; i++) {
135
                printf("\\multicolumn{%d}", decoded.field[i].bitsize);
136
                printf("{%sc|}{%d bits}&\n", (i == 1 ? "|":""), decoded.field[i].bitsize);
137
        }
138
        printf("\\multicolumn{%d}", decoded.field[i].bitsize);
139 87 lampret
        printf("{%sc|}{%d bits}\\\\\n", (i == 1 ? "|":""), decoded.field[i].bitsize);
140
/*      printf("\n\\hline\n");
141
/*      printf("\\multicolumn{32}");
142
/*      printf("{|c|}{\\textcolor{white}{XXXX XXXX XXXX XXXX XXXX XXXX XXXX XXXX XXXX ");
143
        printf("XXXX XXXX XXXX XXXX XXXX XXXX XXXX XXXX XXXX XXXX}}\\\\\n");*/
144
        printf("\\end{tabular}\\par}\n");
145 14 lampret
}
146
 
147 95 lampret
 
148
void print_summary(int newtable, struct or32_opcode *insn, struct or1k_isa *info)
149
{
150
        char tmp[2000];
151
        char tmp2[2000];
152
        int i, j, k;
153
 
154
 
155
        if (newtable) {
156
 
157
                printf("{\\centering \\begin{tabular}{|l");
158
 
159
                /* organization of the table */
160
                for(i = 1; i <= decoded.fldcnt; i++) {
161
                        printf("|");
162
                        for(j = 0; j < decoded.field[i].bitsize; j++)
163
                                if (j == 0)
164
                                        printf("l");
165
                                else if (j+1 == decoded.field[i].bitsize)
166
                                        printf("r");
167
                                else
168
                                        printf("c");
169
                }
170
                printf("|}\n");
171
                printf("Insn&\n");
172
 
173
                /* print first row of the table */
174
                for(i = decoded.insn_size - 1; i > 0; i--)
175
                        if (decoded.bitpos[i])
176
                                printf("%d&\n", i);
177
                        else
178
                                printf("%d&\n");
179
                printf("0\\\\\n");
180
        }
181
        else {
182
                printf("\\multicolumn{1}{|c}{}&\n");
183
                k = 31;
184
                for(i = 1; i <= decoded.fldcnt; i++) {
185
                        for(j = 0; j < decoded.field[i].bitsize; j++) {
186
                                if (j == 0)
187
                                        printf("\\multicolumn{1}{|l");
188
                                else if (j+1 == decoded.field[i].bitsize)
189
                                        printf("\\multicolumn{1}{|r");
190
                                else
191
                                        printf("\\multicolumn{1}{|c");
192
                                if (k)
193
                                        printf("}{%d}&\n", k);
194
                                else
195
                                        printf("}{%d}\\\\\n", k);
196
                                k--;
197
                        }
198
                }
199
        }
200
 
201
        printf("\\multicolumn{1}{|l|}{");
202
        transform_tex(info->name, tmp);
203
        printf("%s}&\n", tmp);
204
 
205
        /* print second row of the table */
206
        for(i = 1; i <= decoded.fldcnt; i++) {
207
                if (decoded.field[i].type == opcode) {
208
                        printf("\\multicolumn{%d}{", decoded.field[i].bitsize);
209
                        printf("%s", (i == 1 ? "|":""));
210
                        printf("c|}{opcode 0x%x}", decoded.field[i].value);
211
                } else if (decoded.field[i].type == operand) {
212
                        printf("\\multicolumn{%d}{", decoded.field[i].bitsize);
213
                        printf("%s", (i == 1 ? "|":""));
214
                        printf("c|}{%c}", decoded.field[i].value);
215
                } else if (decoded.field[i].type == reserved) {
216
                        printf("\\multicolumn{%d}{", decoded.field[i].bitsize);
217
                        printf("%s", (i == 1 ? "|":""));
218
                        printf("c|}{reserved}", decoded.field[i].value);
219
                }
220
                if (i == decoded.fldcnt)
221
                        printf("\\\\\n");
222
                else
223
                        printf("&\n");
224
        }
225
        printf("\n");
226
 
227
        /* print third row of the table */
228
/*      printf("\\multicolumn{10}{|c|}{");
229
        transform_tex(info->name, tmp);
230
        printf("%s ", tmp);
231
        transform_tex(insn->args, tmp);
232
        printf("%s}&\n", tmp);
233
        printf("\\multicolumn{22}{c|}{");
234
        eliminate_crcr(info->desc, tmp2);
235
        transform_tex(tmp2, tmp);
236
        printf("%s}\\\\\n", tmp); */
237
 
238
 
239
}
240
 
241 87 lampret
void decode(struct or32_opcode *insn)
242 14 lampret
{
243
        int opc_pos = 0;
244
        char *enc;
245
        encfld_types last;
246
        char lastoperand;
247
        int tmp;
248
 
249
        if (!insn) {
250
                printf("internal error: insn pointer NULL\n");
251
                return;
252
        }
253
 
254
        memset(&decoded, 0, sizeof(decoded));
255
 
256
        if ((insn->name[0] == 'h') && (insn->name[1] == '.'))
257
                decoded.insn_size = opc_pos = 16;
258
        else
259
                decoded.insn_size = opc_pos = 32;
260
 
261
        last = -1;
262
 
263
        for (enc = insn->encoding; *enc != '\0'; )
264
                if ((*enc == '0') && (*(enc+1) == 'x')) {
265
                        int tmp = strtol(enc, NULL, 16);
266
                        if (last != opcode) {
267
                                decoded.bitpos[opc_pos] = 1;
268
                                decoded.bitpos[opc_pos - 1] = 1;
269
                                decoded.field[++decoded.fldcnt].type = opcode;
270
                        }
271
                        decoded.field[decoded.fldcnt].value <<= 4;
272
                        decoded.field[decoded.fldcnt].value += tmp;
273
                        decoded.field[decoded.fldcnt].bitsize += 4;
274
                        opc_pos -= 4;
275
                        enc += 3;
276
                        last = opcode;
277
                }
278
                else if (*enc == '0') {
279
                       if (last != opcode) {
280
                                decoded.bitpos[opc_pos] = 1;
281
                                decoded.bitpos[opc_pos - 1] = 1;
282
                                decoded.field[++decoded.fldcnt].type = opcode;
283
                        }
284
                        decoded.field[decoded.fldcnt].value <<= 1;
285
                        decoded.field[decoded.fldcnt].bitsize += 1;
286
                        opc_pos--;
287
                        enc++;
288
                        last = opcode;
289
                }
290
                else if (*enc == '-') {
291
                       if (last != reserved) {
292
                                decoded.bitpos[opc_pos] = 1;
293
                                decoded.bitpos[opc_pos - 1] = 1;
294
                                decoded.field[++decoded.fldcnt].type = reserved;
295
                        }
296
                        decoded.field[decoded.fldcnt].value <<= 1;
297
                        decoded.field[decoded.fldcnt].bitsize += 1;
298
                        opc_pos--;
299
                        enc++;
300
                        last = reserved;
301
                }
302
                else if (*enc == '1') {
303
                       if (last != opcode) {
304
                                decoded.bitpos[opc_pos] = 1;
305
                                decoded.bitpos[opc_pos - 1] = 1;
306
                                decoded.field[++decoded.fldcnt].type = opcode;
307
                        }
308
                        decoded.field[decoded.fldcnt].value <<= 1;
309
                        decoded.field[decoded.fldcnt].value += 1;
310
                        decoded.field[decoded.fldcnt].bitsize += 1;
311
                        opc_pos--;
312
                        enc++;
313
                        last = opcode;
314
                }
315
                else if (isalpha(*enc)) {
316
                       if ((last != operand) || (lastoperand != *enc)) {
317
                                decoded.bitpos[opc_pos] = 1;
318
                                decoded.bitpos[opc_pos - 1] = 1;
319
                                decoded.field[++decoded.fldcnt].type = operand;
320
                        }
321
                        decoded.field[decoded.fldcnt].value = *enc;
322
                        decoded.field[decoded.fldcnt].bitsize += 1;
323
                        opc_pos--;
324
                        lastoperand = *enc;
325
                        enc++;
326
                        last = operand;
327
                }
328
                else
329
                        enc++;
330
}
331
 
332 87 lampret
void print_header(struct or32_opcode *insn, struct or1k_isa *info)
333 14 lampret
{
334 87 lampret
        char tmp[2000];
335
 
336
        printf("\n\n\\pagebreak\n");
337 14 lampret
        printf("\\vspace{10mm}\n");
338
        /* printf("\\section{Appendix A}\n"); */
339
        printf("\\lyxline{\\small}\\vspace{-1\\parskip}\n");
340
        printf("\\vspace{10mm}\n");
341 87 lampret
        printf("{\\centering \\begin{tabular}{ccc}\n");
342 14 lampret
        printf("\\textbf{\\textcolor{white}{\\small Left}}\\textcolor{white}{\\small }&\n");
343 87 lampret
        printf("\\textcolor{white}{\\small }\\textbf{\\textcolor{white}{\\small Middle \n");
344
        printf("Middle Middle Middle Middle Middle Middle ");
345
        printf("Middle Middle Middle Middle Middle Middle ");
346
        printf("Middle Middle Middle}} \\textcolor{white}{\\small }&\n");
347 14 lampret
        printf("\\textcolor{white}{\\small }\\textbf{\\textcolor{white}{\\small Right}}\\\\\n");
348 87 lampret
        transform_tex(insn->name, tmp);
349
        printf("\\textbf{\\Large %s}&\n", tmp);
350
        transform_tex(info->title, tmp);
351
        printf("\\multicolumn{1}{c}{\\textbf{\\Large %s}}&\n", tmp);
352
        transform_tex(insn->name, tmp);
353
        printf("\\textbf{\\Large %s}\\\\\n", tmp);
354 14 lampret
        printf("\\end{tabular}\\par}\n\\bigskip{}\n\n");
355
}
356
 
357 87 lampret
struct or1k_isa *get_or1k_isa(unsigned long classes, struct info *info)
358 14 lampret
{
359
        int i;
360 87 lampret
        struct or1k_isa *tmp;
361
 
362
        while (1) {
363
                /* Pick first instruction that wasn't printed yet and meets current class constraint */
364
                for (i = 0; strlen(or1k_isa_info[i].name); i++)
365
                        if ((!or1k_isa_info[i].printed) && ((1<<or1k_isa_info[i].class)>>1 & classes))
366
                                break;
367
 
368
                /* Don't return any instruction if they were all already processed or don't meet
369
                   class constraint. */
370
                if (strlen(or1k_isa_info[i].name) == 0) {
371
                        fprintf(stderr, "All instructions that meet class constraint %x were processed.\n", classes);
372
                        return NULL;
373
                }
374 14 lampret
 
375 87 lampret
                tmp = &or1k_isa_info[i];
376 14 lampret
 
377 87 lampret
                /* Check if tmp instruction is first in order to be processed. If preceeding
378
                   instructions are found, they are used instead. */
379
                for (i = 0; strlen(or1k_isa_info[i].name); i++)
380
                        if ((!or1k_isa_info[i].printed) && ((1<<or1k_isa_info[i].class)>>1 & classes) &&
381
                            (strcmp(or1k_isa_info[i].name, tmp->name) < 0))
382
                                tmp = &or1k_isa_info[i];
383
 
384
                tmp->printed = 1;
385
 
386
                /* Find matching encoding. */
387
                for (i = 0; strlen(or32_opcodes[i].name); i++)
388
                        if (strcmp(tmp->name, or32_opcodes[i].name) == 0) {
389
                                fprintf(stderr, "PROCESSING: %s\n", tmp->name);
390
                                info->desc = tmp;
391
                                info->opcode = &or32_opcodes[i];
392
                                return tmp;
393
                        }
394
        }
395
 
396
        return NULL;
397 14 lampret
}
398
 
399 87 lampret
void cross_ref_check()
400 14 lampret
{
401 87 lampret
        int i, j;
402
 
403
        for (i = 0; strlen(or32_opcodes[i].name); i++) {
404
                for (j = 0; strlen(or1k_isa_info[j].name); j++)
405
                        if (strcmp(or32_opcodes[i].name, or1k_isa_info[j].name) == 0)
406
                                break;
407
                if (strlen(or1k_isa_info[j].name) == 0)
408
                        fprintf(stderr, "Description for %s missing.\n", or32_opcodes[i].name);
409 14 lampret
        }
410 87 lampret
 
411
        for (i = 0; strlen(or1k_isa_info[i].name); i++) {
412
                for (j = 0; strlen(or32_opcodes[j].name); j++)
413
                        if (strcmp(or1k_isa_info[i].name, or32_opcodes[j].name) == 0)
414
                                break;
415
                if (strlen(or32_opcodes[j].name) == 0)
416
                        fprintf(stderr, "Encoding for %s missing.\n", or1k_isa_info[i].name);
417
        }
418 14 lampret
}
419
 
420 87 lampret
void print_body(struct or32_opcode *insn, struct or1k_isa *info)
421 14 lampret
{
422
        char tmp[2000];
423
 
424
        printf("\\vspace{15mm}\n");
425
 
426 87 lampret
        printf("{\\par \\raggedright \\textbf{\\Large Format:}\\Large \\par}\n");
427 14 lampret
        printf("\\vspace{5mm}\n");
428
        printf("\\begin{quotation}\n");
429 87 lampret
        transform_tex(info->name, tmp);
430
        printf("\\texttt{\\large %s\\ ", tmp);
431
        transform_tex(insn->args, tmp);
432
        printf("%s}{\\large \\par}\n", tmp);
433 14 lampret
        printf("\\end{quotation}\n");
434
        printf("\\vspace{10mm}\n");
435
 
436 87 lampret
        printf("\\textbf{\\Large Description:}{\\Large \\par}\n");
437 14 lampret
        printf("\\vspace{5mm}\n");
438
        printf("\\begin{quotation}\n");
439 87 lampret
        transform_tex(info->desc, tmp);
440
        printf("%s\n", tmp);
441 14 lampret
        printf("\\end{quotation}\n");
442
        printf("\\vspace{10mm}\n");
443
 
444 89 lampret
        printf("\\textbf{\\Large 32-bit Implementation:}{\\Large \\par}\n");
445 14 lampret
        printf("\\vspace{5mm}\n");
446 87 lampret
        printf("\\begin{flushleft}\n");
447 14 lampret
        printf("\\begin{quotation}\n");
448 87 lampret
        transform_tex(info->oper32, tmp);
449
        printf("\\texttt{\\large %s}{\\large \\par}\n", tmp);
450 14 lampret
        printf("\\end{quotation}\n");
451 87 lampret
        printf("\\end{flushleft}\n");
452 14 lampret
        printf("\\vspace{10mm}\n");
453
 
454 89 lampret
        printf("\\textbf{\\Large 64-bit Implementation:}{\\Large \\par}\n");
455 14 lampret
        printf("\\vspace{5mm}\n");
456 87 lampret
        printf("\\begin{flushleft}\n");
457 14 lampret
        printf("\\begin{quotation}\n");
458 87 lampret
        transform_tex(info->oper64, tmp);
459
        printf("\\texttt{\\large %s}{\\large \\par}\n", tmp);
460
        printf("\\end{quotation}\n");
461
        printf("\\end{flushleft}\n");
462
        printf("\\vspace{10mm}\n");
463
 
464
        printf("\\textbf{\\Large Exceptions:}{\\Large \\par}\n");
465
        printf("\\vspace{5mm}\n");
466
        printf("\\begin{flushleft}\n");
467
        printf("\\begin{quotation}\n");
468
        transform_tex(info->except, tmp);
469
        printf("\\texttt{\\large %s}{\\large \\par}\n",tmp);
470
        printf("\\end{quotation}\n");
471
        printf("\\end{flushleft}\n");
472
        printf("\\vspace{10mm}\n");
473
 
474
        printf("\\textbf{\\Large Notes:}{\\Large \\par}\n");
475
        printf("\\vspace{5mm}\n");
476
        printf("\\begin{quotation}\n");
477 14 lampret
        printf("\n");
478
        printf("\\end{quotation}\n");
479
        printf("\\vspace{10mm}\n");
480
 
481
        printf("\\vfill\n");
482 87 lampret
        printf("{\\centering \\begin{tabular}{|c|c|}\n");
483 14 lampret
        printf("\\hline\n");
484 89 lampret
        printf("Instruction Class&\n");
485 14 lampret
        printf("Implementation\\\\\n");
486
        printf("\\hline\n");
487 87 lampret
        transform_tex(or1k_isa_classes[info->class].table, tmp);
488
        printf("%s\n", tmp);
489 14 lampret
        printf("\\end{tabular}\\par}\n");
490 87 lampret
 
491 14 lampret
        printf("\n");
492
}
493
 
494 95 lampret
int main(int argc, char *argv[])
495 14 lampret
{
496 95 lampret
        int i, j;
497
        int summary = 0;
498
        long classes = -1;
499
 
500
        if ((argc >= 2) && (strcmp(argv[1], "-s") == 0))
501
                summary = 1;
502 14 lampret
 
503 95 lampret
        if (argc == 3)
504
                classes = strtol(argv[2], NULL, 0);
505
 
506 87 lampret
        cross_ref_check();
507 14 lampret
        printf("\n\\begin{document}\n\\vspace{50mm}");
508
 
509 87 lampret
        for(i = 0; strlen(or1k_order[i].title); i++) {
510
                printf("\\section{%s}\n", or1k_order[i].title);
511 95 lampret
                j= 0;
512
 
513
                while (get_or1k_isa(or1k_order[i].classes, &info)) {
514
                        if (((1 << info.desc->class) & classes) == 0)
515
                                continue;
516 87 lampret
 
517 95 lampret
                        if (summary) {
518
                                decode(info.opcode);
519
                                if (j && ((j % 100) == 0))
520
                                        printf("\\end{tabular}\\par}\n");
521
                                print_summary(((j % 100) == 0), info.opcode, info.desc);
522
                        } else {
523
                                print_header(info.opcode, info.desc);
524
                                decode(info.opcode);
525
                                print_encoding();
526
                                print_body(info.opcode, info.desc);
527
                        }
528
                        j++;
529 87 lampret
                }
530 95 lampret
                if (summary && j)
531
                        printf("\\end{tabular}\\par}\n");
532 87 lampret
        }
533 14 lampret
 
534
        printf("\n\\end{document}\n");
535
        return 0;
536
}

powered by: WebSVN 2.1.0

© copyright 1999-2025 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.