Browse files

Refactor.

  • Loading branch information...
1 parent fee106a commit fd5ab72c95a43f489fab1d851bfaef4b5b8f7fd1 @kr committed Mar 13, 2009
Showing with 39 additions and 43 deletions.
  1. +1 −1 .gitignore
  2. +3 −3 Makefile
  3. +7 −14 gen-mod-index
  4. +11 −0 index.h
  5. +0 −13 module-index.h
  6. +5 −5 module.na
  7. +12 −7 vm.c
View
2 .gitignore
@@ -8,7 +8,7 @@
gmon.out
core
core.*
-module-index.c
+index.c
prelude.c
vm
sh-tests/*.out
View
6 Makefile
@@ -14,7 +14,7 @@ namodules := \
symbol.na \
cmodules := vm.c mem.c gen.c prim.c
-sources := $(cmodules) module-index.c $(namodules:.na=.na.c)
+sources := $(cmodules) index.c $(namodules:.na=.na.c)
export CFLAGS := -g -pg -Wall -Werror
#export CFLAGS := -O2 -Wall -Werror
@@ -30,7 +30,7 @@ endif
vm: $(sources:.c=.o)
-module-index.c: $(namodules)
+index.c: $(namodules)
./gen-mod-index --output=$@ $(namodules:.na=)
%.na: ;
@@ -48,7 +48,7 @@ clean:
rm -f vm *.o core core.* gmon.out sh-tests/*.out *.d *.pyc
rm -f *.lxc ad-hoc-tests/*.lxc lib/*.lxc tests/*.lxc
rm -f *.nac ad-hoc-tests/*.nac lib/*.nac tests/*.nac
- rm -f *.na.c module-index.c
+ rm -f *.na.c index.c
# .DELETE_ON_ERROR:
.PHONY: all clean distclean reallyclean check
View
21 gen-mod-index
@@ -19,36 +19,29 @@ for opt, val in opts:
if opt in ('--output', '-o'):
outfile = val
-if not outfile.endswith('.c'):
- outfile = outfile + '.c'
-
-lxmodules = []
+modules = []
for arg in args:
- lxmodules.append(arg)
-
-count = len(lxmodules)
+ modules.append(arg)
outfd = open(outfile, 'w')
print >>outfd, '/* This file is automatically generated. */'
print >>outfd
print >>outfd, '#include "lxc.h"'
-print >>outfd, '#include "module-index.h"'
-#print >>outfd, 'extern struct lxc_module lxc_module_prelude;'
-for module in lxmodules:
+print >>outfd, '#include "index.h"'
+for module in modules:
module = tr(module, '/-.', '___')
if match('.*[^-_/A-Za-z0-9]', module): raise 'bad module name', module
print >>outfd, 'extern struct lxc_module lxc_module_%s;' % (module,)
print >>outfd
-print >>outfd, 'int lxc_modules_count = %d;' % (count,)
-
-print >>outfd, 'lxc_module lxc_modules[%d] = {' % (count,)
+print >>outfd, 'lxc_module lxc_modules[%d] = {' % (len(modules) + 1)
#print >>outfd, ' &lxc_module_prelude,'
-for module in lxmodules:
+for module in modules:
module = tr(module, '/-.', '___')
if match('.*[^-_/A-Za-z0-9]', module): raise 'bad module name', module
print >>outfd, ' &lxc_module_%s,' % (module,)
+print >>outfd, ' 0,'
print >>outfd, '};'
View
11 index.h
@@ -0,0 +1,11 @@
+/* index.h - module index header (static) */
+
+#ifndef index_h
+#define index_h
+
+#include "lxc.h"
+
+extern lxc_module lxc_modules[];
+
+#endif /*index_h*/
+
View
13 module-index.h
@@ -1,13 +0,0 @@
-/* module-index.h - module index header (static) */
-
-#ifndef module_index_h
-#define module_index_h
-
-#include "lxc.h"
-
-extern int lxc_modules_count;
-
-extern lxc_module lxc_modules[];
-
-#endif /*module_index_h*/
-
View
10 module.na
@@ -4,21 +4,21 @@ inline C <<end
#include "str.h"
#include "bytes.h"
-#include "module-index.h"
+#include "index.h"
const datum module_mtab = mtab;
static datum
find_builtin_module(datum name)
{
- int i;
+ lxc_module mod, *modp = lxc_modules;
if (symbolp(name)) name = (datum) name[1]; /* symbol->str */
- for (i = 0; i < lxc_modules_count; i++) {
- const char *mname = lxc_modules[i]->name;
+ while ((mod = *modp++)) {
+ const char *mname = mod->name;
if (str_cmp_charstar(name, strlen(mname), mname) == 0) {
- return lxc_modules[i]->instrs;
+ return mod->instrs;
}
}
View
19 vm.c
@@ -23,7 +23,7 @@
#include "nil.h"
#include "int.h"
#include "config.h"
-#include "module-index.h"
+#include "index.h"
#define OP_NOP 0x00
#define OP_unused1 0x01
@@ -571,10 +571,19 @@ report_error(datum args)
return die1("error", args);
}
+void
+link_builtins(lxc_module *modp)
+{
+ lxc_module mod;
+ while ((mod = *modp++)) {
+ nalink(mod->instrs, mod->instrs_count,
+ mod->str_offsets, mod->ime_offsets, mod->sym_offsets);
+ }
+}
+
int
main(int argc, char **argv)
{
- size_t i;
datum args;
if (argc != 2) usage();
@@ -584,11 +593,7 @@ main(int argc, char **argv)
str_init();
bytes_init();
- for (i = 0; i < lxc_modules_count; i++) {
- lxc_module mod = lxc_modules[i];
- nalink(mod->instrs, mod->instrs_count,
- mod->str_offsets, mod->ime_offsets, mod->sym_offsets);
- }
+ link_builtins(lxc_modules);
genv = cons(nil, nil);

0 comments on commit fd5ab72

Please sign in to comment.