1 
2 #include "config.h"
3 
4 #include <orc/orc.h>
5 #include <orc/orcparse.h>
6 
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <string.h>
10 #include <ctype.h>
11 
12 
13 int verbose = 0;
14 int error = 0;
15 int compat;
16 
17 char *target = "sse";
18 
19 #define ORC_VERSION(a,b,c,d) ((a)*1000000 + (b)*10000 + (c)*100 + (d))
20 #define REQUIRE(a,b,c,d) do { \
21   if (ORC_VERSION((a),(b),(c),(d)) > compat) { \
22     fprintf(stderr, "Feature used that is incompatible with --compat\n"); \
23     exit (1); \
24   } \
25 } while (0)
26 
help(void)27 void help (void)
28 {
29   printf("Usage:\n");
30   printf("  generate-bytecode [OPTION...]\n");
31   printf("\n");
32   printf("Help Options:\n");
33   printf("  -h, --help              Show help options\n");
34   printf("\n");
35   printf("Application Options:\n");
36   printf("  -o, --output FILE       Write output to FILE\n");
37   printf("      --header            Write header instead of .c file\n");
38   printf("\n");
39 
40   exit (0);
41 }
42 
43 int
main(int argc,char * argv[])44 main (int argc, char *argv[])
45 {
46   char *output_file = NULL;
47   char *input_file = NULL;
48   FILE *output;
49   int i;
50   OrcOpcodeSet *opcode_set;
51   int output_header = FALSE;
52 
53   orc_init ();
54 
55   for(i=1;i<argc;i++) {
56     if (strcmp (argv[i], "--output") == 0 ||
57         strcmp(argv[i], "-o") == 0) {
58       if (i+1 < argc) {
59         output_file = argv[i+1];
60         i++;
61       } else {
62         help();
63       }
64     } else if (strcmp (argv[i], "--header") == 0) {
65       output_header = TRUE;
66     } else if (strncmp(argv[i], "-", 1) == 0) {
67       printf("Unknown option: %s\n", argv[i]);
68       exit (1);
69     } else {
70       if (input_file == NULL) {
71         input_file = argv[i];
72       } else {
73         printf("More than one input file specified: %s\n", argv[i]);
74         exit (1);
75       }
76     }
77   }
78 
79   if (output_file == NULL) {
80     output_file = output_header ? "out.h" : "out.c";
81     printf("Writing to file: %s\n", output_file);
82   }
83 
84   output = fopen (output_file, "w");
85   if (!output) {
86     printf("Could not write output file: %s\n", output_file);
87     exit(1);
88   }
89 
90   opcode_set = orc_opcode_set_get ("sys");
91 
92   fprintf(output, "\n");
93   fprintf(output, "/* autogenerated by generate-bytecode */\n");
94   fprintf(output, "\n");
95 
96   if (output_header) {
97     fprintf(output, "#include <math.h>\n");
98     fprintf(output, "#include <orc/orc.h>\n");
99     fprintf(output, "\n");
100 
101     fprintf(output, "typedef enum {\n");
102     fprintf(output, "  ORC_BC_END,\n");
103     fprintf(output, "  ORC_BC_BEGIN_FUNCTION,\n");
104     fprintf(output, "  ORC_BC_END_FUNCTION,\n");
105     fprintf(output, "  ORC_BC_SET_CONSTANT_N,\n");
106     fprintf(output, "  ORC_BC_SET_N_MULTIPLE,\n");
107     fprintf(output, "  ORC_BC_SET_N_MINIMUM,\n");
108     fprintf(output, "  ORC_BC_SET_N_MAXIMUM,\n");
109     fprintf(output, "  ORC_BC_SET_2D,\n");
110     fprintf(output, "  ORC_BC_SET_CONSTANT_M,\n");
111     fprintf(output, "  ORC_BC_SET_NAME,\n");
112     fprintf(output, "  ORC_BC_SET_BACKUP_FUNCTION,\n");
113     fprintf(output, "  ORC_BC_ADD_DESTINATION,\n");
114     fprintf(output, "  ORC_BC_ADD_SOURCE,\n");
115     fprintf(output, "  ORC_BC_ADD_ACCUMULATOR,\n");
116     fprintf(output, "  ORC_BC_ADD_CONSTANT,\n");
117     fprintf(output, "  ORC_BC_ADD_CONSTANT_INT64,\n");
118     fprintf(output, "  ORC_BC_ADD_PARAMETER,\n");
119     fprintf(output, "  ORC_BC_ADD_PARAMETER_FLOAT,\n");
120     fprintf(output, "  ORC_BC_ADD_PARAMETER_INT64,\n");
121     fprintf(output, "  ORC_BC_ADD_PARAMETER_DOUBLE,\n");
122     fprintf(output, "  ORC_BC_ADD_TEMPORARY,\n");
123     fprintf(output, "  ORC_BC_INSTRUCTION_FLAGS,\n");
124     for (i=22;i<32;i++){
125       fprintf(output, "  ORC_BC_RESERVED_%d,\n", i);
126     }
127     for(i=0;i<opcode_set->n_opcodes;i++){
128       OrcStaticOpcode *opcode = opcode_set->opcodes + i;
129 
130       if ((i+32)%10 == 0) {
131         fprintf(output, "  /* %d */\n", i+32);
132       }
133       fprintf(output, "  ORC_BC_%s,\n", opcode->name);
134 
135     }
136     fprintf(output, "  /* %d */\n", i+32);
137     fprintf(output, "  ORC_BC_LAST\n");
138     fprintf(output, "} OrcBytecodes;\n");
139   } else {
140     fprintf(output, "#ifdef HAVE_CONFIG_H\n");
141     fprintf(output, "#include \"config.h\"\n");
142     fprintf(output, "#endif\n");
143 
144     fprintf(output, "  ORC_BC_END,\n");
145     fprintf(output, "  ORC_BC_BEGIN_FUNCTION,\n");
146     fprintf(output, "  ORC_BC_END_FUNCTION,\n");
147     fprintf(output, "  ORC_BC_SET_CONSTANT_N,\n");
148     fprintf(output, "  ORC_BC_SET_N_MULTIPLE,\n");
149     fprintf(output, "  ORC_BC_SET_N_MINIMUM,\n");
150     fprintf(output, "  ORC_BC_SET_N_MAXIMUM,\n");
151     fprintf(output, "  ORC_BC_SET_2D,\n");
152     fprintf(output, "  ORC_BC_SET_CONSTANT_M,\n");
153     fprintf(output, "  ORC_BC_SET_NAME,\n");
154     fprintf(output, "  ORC_BC_SET_BACKUP_FUNCTION,\n");
155     fprintf(output, "  ORC_BC_ADD_DESTINATION,\n");
156     fprintf(output, "  ORC_BC_ADD_SOURCE,\n");
157     fprintf(output, "  ORC_BC_ADD_ACCUMULATOR,\n");
158     fprintf(output, "  ORC_BC_ADD_CONSTANT,\n");
159     fprintf(output, "  ORC_BC_ADD_CONSTANT_INT64,\n");
160     fprintf(output, "  ORC_BC_ADD_PARAMETER,\n");
161     fprintf(output, "  ORC_BC_ADD_PARAMETER_FLOAT,\n");
162     fprintf(output, "  ORC_BC_ADD_PARAMETER_INT64,\n");
163     fprintf(output, "  ORC_BC_ADD_PARAMETER_DOUBLE,\n");
164     fprintf(output, "  ORC_BC_ADD_TEMPORARY,\n");
165     fprintf(output, "  ORC_BC_INSTRUCTION_FLAGS,\n");
166     for (i=22;i<32;i++){
167       fprintf(output, "  ORC_BC_RESERVED_%d,\n", i);
168     }
169 
170     for(i=0;i<opcode_set->n_opcodes;i++){
171       OrcStaticOpcode *opcode = opcode_set->opcodes + i;
172 
173       if ((i+32)%10 == 0) {
174         fprintf(output, "  /* %d */\n", i+32);
175       }
176       fprintf(output, "  { \"%s\" },\n", opcode->name);
177 
178     }
179     fprintf(output, "  /* %d */\n", i+32);
180     fprintf(output, "  ORC_BC_LAST\n");
181     fprintf(output, "} OrcBytecodes;\n");
182   }
183 
184   fclose (output);
185 
186   if (error) exit(1);
187 
188   return 0;
189 }
190 
191 
192