File: mkvmi.c

package info (click to toggle)
swi-prolog 6.6.6-1~bpo70+1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy-backports
  • size: 82,312 kB
  • sloc: ansic: 322,250; perl: 245,822; sh: 6,651; java: 5,254; makefile: 4,423; cpp: 4,153; ruby: 1,594; yacc: 843; xml: 82; sed: 12; sql: 6
file content (390 lines) | stat: -rw-r--r-- 8,645 bytes parent folder | download | duplicates (3)
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
/*  $Id$

    Part of SWI-Prolog

    Author:        Jan Wielemaker
    E-mail:        J.Wielemaker@uva.nl
    WWW:           http://www.swi-prolog.org
    Copyright (C): 2008, University of Amsterdam

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 2.1 of the License, or (at your option) any later version.

    This library 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
    Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public
    License along with this library; if not, write to the Free Software
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#ifdef __WINDOWS__
#include <direct.h>
#else
#include <unistd.h>
#endif

#define NO_SWIPL 1
#include "pl-hash.c"

/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
This program creates pl-codetable.c, pl-jumptable.ic   and pl-vmi.h from
pl-vmi.c.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

const char *program;
int verbose = 0;
const char *vmi_file    = "pl-vmi.c";
const char *ctable_file = "pl-codetable.c";
const char *jump_table  = "pl-jumptable.ic";
const char *vmi_hdr	= "pl-vmi.h";

#define MAX_VMI 1000

typedef struct				/* VMI( */
{ char *name;				/* Name */
  char *flags;				/* Flags (VIF_*) */
  char *argc;				/* Argument length (or VM_DYNARGC) */
  char *args;				/* Argument types (max 3) */
} vmi;					/* ) */

vmi vmi_list[MAX_VMI];
int vmi_count = 0;

char *synopsis;
size_t syn_size = 0;
size_t syn_allocated = 0;

static void
add_synopsis(const char *s, size_t len)
{ if ( syn_size+len+1 > syn_allocated )
  { if ( syn_allocated == 0 )
    { syn_allocated = 1024;
      synopsis = malloc(syn_allocated);
      syn_size = 0;
    } else
    { syn_allocated *= 2;
      synopsis = realloc(synopsis, syn_allocated);
    }
  }

  strncpy(&synopsis[syn_size], s, len);
  syn_size+=len;
  synopsis[syn_size++]='&';
}


static char *
skip_ws(const char *s)
{ if ( s )
  { for (; *s; s++)
    { if ( *s != ' ' && *s != '\t' )
	return (char*)s;
    }
  }

  return NULL;
}


static char *
skip_id(const char *s)
{ if ( s )
  { for (; *s; s++)
    { if ( *s == '_' ||
	   (*s >= 'A' && *s <= 'Z') ||
	   (*s >= '0' && *s <= '9') )
	continue;

      return (char*)s;
    }
  }

  return NULL;
}


static char *
skip_flags(const char *s)
{ if ( s )
  { for (; *s; s++)
    { if ( *s == '_' || *s == '|' ||
	   (*s >= 'A' && *s <= 'Z') ||
	   (*s >= '0' && *s <= '9') )
	continue;

      return (char*)s;
    }
  }

  return NULL;
}


static char *
skip_over(const char *s, int c)
{ if ( s )
  { for (; *s; s++)
    { if ( *s == c )
	return (char*)(s+1);
    }
  }

  return NULL;
}


static char *				/* not always around */
my_strndup(const char *in, size_t len)
{ char *s = malloc(len+1);

  strncpy(s, in, len);
  s[len] = '\0';

  return s;
}


static int
load_vmis(const char *file)
{ FILE *fd = fopen(file, "r");

  if ( fd )
  { char buf[1024];
    int line = 0;

    while(fgets(buf, sizeof(buf), fd))
    { line++;

      if ( strncmp(buf, "VMI(", 4) == 0 )
      { const char *s1 = skip_ws(buf+4);
	const char *e1 = skip_id(s1);
	const char *s2 = skip_ws(skip_over(e1, ','));
	const char *e2 = skip_flags(s2);
	const char *s3 = skip_ws(skip_over(e2, ','));
	const char *e3 = skip_flags(s3);
	const char *s4 = skip_over(skip_ws(skip_over(e3, ',')), '(');
	const char *e4 = skip_over(s4, ')');

	if ( !e4 )
	{ fprintf(stderr, "Syntax error at %s:%d\n", file, line);
	  exit(1);
	} else
	  e4--;				/* backspace over ) */

	vmi_list[vmi_count].name  = my_strndup(s1, e1-s1);
	vmi_list[vmi_count].flags = my_strndup(s2, e2-s2);
	vmi_list[vmi_count].argc  = my_strndup(s3, e3-s3);
	vmi_list[vmi_count].args  = my_strndup(s4, e4-s4);

	add_synopsis(s1, e1-s1);	/* flags (s2) isn't needed for VM signature */
	add_synopsis(s3, e3-s3);
	add_synopsis(s4, e4-s4);

	vmi_count++;
      }
    }

    fclose(fd);
    return 0;
  }

  return -1;
}


static int
cmp_file(const char *from, const char *to)
{ FILE *f1 = fopen(from, "r");
  FILE *f2 = fopen(to, "r");
  int rc = -1;

  if ( f1 && f2 )
  { char l1[1024];
    char l2[1024];

    while ( fgets(l1, sizeof(l1), f1) )
    { if ( fgets(l2, sizeof(l2), f2) )
      { if ( strcmp(l1, l2) == 0 )
	  continue;
      }
      goto out;
    }
    if ( !fgets(l2, sizeof(l2), f2) )
      rc = 0;
  }

out:
  if ( f1 ) fclose(f1);
  if ( f2 ) fclose(f2);
  return rc;
}


static int
update_file(const char *from, const char *to)
{ if ( cmp_file(from, to) == 0 )
  { if ( verbose )
      fprintf(stderr, "\t%s: no change\n", to);
    return remove(from);
  } else
  { remove(to);
    if ( verbose )
      fprintf(stderr, "\t%s: updated\n", to);
    return rename(from, to);
  }
}


static char *
mystrlwr(char *to, const char *from)	/* my*: Windows name conflict */
{ char *s;

  for(s=to; *from; from++)
  { *s++ = tolower(*from);
  }
  *s = '\0';

  return to;
}


static int
emit_code_table(const char *to)
{ const char *tmp = "vmi.tmp";
  FILE *out = fopen(tmp, "w");
  int i;

  fprintf(out, "/*  File: %s\n\n", to);
  fprintf(out, "    This file provides a description of the virtual machine instructions\n");
  fprintf(out, "    and their arguments. It  is  used by  pl-comp.c  to facilitate the\n");
  fprintf(out, "    compiler and decompiler, as well as pl-wic.c to save/reload sequences\n");
  fprintf(out, "    of virtual machine qinstructions.\n");
  fprintf(out, "\n");
  fprintf(out, "    Note: this file is generated by %s from %s.  DO NOT EDIT", program, vmi_file);
  fprintf(out, "    \n");
  fprintf(out, "*/\n\n");
  fprintf(out, "#include \"pl-incl.h\"\n\n");
  fprintf(out, "const code_info codeTable[] = {\n");
  fprintf(out, "  /* {name, ID, flags, #args, argtype} */\n");

  for(i=0; i<vmi_count; i++)
  { char name[100];

    fprintf(out, "  {\"%s\", %s, %s, %s, {%s}},\n",
	    mystrlwr(name, vmi_list[i].name),
	    vmi_list[i].name,
	    vmi_list[i].flags,
	    vmi_list[i].argc,
	    vmi_list[i].args[0] ? vmi_list[i].args : "0");
  }

  fprintf(out, "  { NULL, 0, 0, 0, {0} }\n");
  fprintf(out, "};\n");
  fclose(out);

  return update_file(tmp, to);
}


static int
emit_jump_table(const char *to)
{  const char *tmp = "vmi.tmp";
  FILE *out = fopen(tmp, "w");
  int i;

  fprintf(out, "/*  File: %s\n\n", to);
  fprintf(out, "    This file provides the GCC-2 jump-labels to exploit GCC's\n");
  fprintf(out, "    support for threaded code.\n");
  fprintf(out, "\n");
  fprintf(out, "    Note: this file is generated by %s from %s.  DO NOT EDIT", program, vmi_file);
  fprintf(out, "    \n");
  fprintf(out, "*/\n\n");

  fprintf(out, "static void *jmp_table[] =\n");
  fprintf(out, "{\n");


  for(i=0; i<vmi_count; i++)
  { fprintf(out, "  &&%s_LBL,\n", vmi_list[i].name);
  }

  fprintf(out, "  NULL\n");
  fprintf(out, "};\n");

  fclose(out);

  return update_file(tmp, to);
}


static int
emit_code_defs(const char *to)
{  const char *tmp = "vmi.tmp";
  FILE *out = fopen(tmp, "w");
  int i;

  fprintf(out, "/*  File: %s\n\n", to);
  fprintf(out, "    This file provides the definition of type code.\n");
  fprintf(out, "\n");
  fprintf(out, "    Note: this file is generated by %s from %s.  DO NOT EDIT", program, vmi_file);
  fprintf(out, "    \n");
  fprintf(out, "*/\n\n");

  fprintf(out, "typedef enum\n");
  fprintf(out, "{\n");


  for(i=0; i<vmi_count; i++)
  { fprintf(out, "  %s,\n", vmi_list[i].name);
  }

  fprintf(out, "  VMI_END_LIST\n");
  fprintf(out, "} vmi;\n\n");
  fprintf(out, "#define I_HIGHEST ((int)VMI_END_LIST)\n");
  fprintf(out, "#define VM_SIGNATURE 0x%x\n", MurmurHashAligned2(synopsis, syn_size, 0x12345678));

  fclose(out);

  return update_file(tmp, to);
}

int
main(int argc, char **argv)
{ program = argv[0];

  argc--;
  argv++;

  if ( argc >= 1 && strcmp(argv[0], "-v") == 0 )
  { argc--;
    argv++;
    verbose = 1;
  }

  if ( argc == 1 )
  { if ( chdir(argv[0]) )
    { fprintf(stderr, "%s: Could not chdir to %s\n", argv[0], argv[0]);
      exit(1);
    }
  }

  load_vmis(vmi_file);
  if ( verbose )
    fprintf(stderr, "Found %d VMs\n", vmi_count);

  if ( emit_code_table(ctable_file) == 0 &&
       emit_jump_table(jump_table) == 0 &&
       emit_code_defs(vmi_hdr) == 0 )
    return 0;
  else
    return 1;
}