summaryrefslogtreecommitdiffstats
path: root/qemu/roms/openbios/kernel/bootstrap.c
blob: 520d7b48c13e2ee14460ef5162d9803d956fc8f8 (plain)
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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
/* tag: forth bootstrap environment
 *
 * Copyright (C) 2003-2006 Stefan Reinauer, Patrick Mauritz
 *
 * See the file "COPYING" for further information about
 * the copyright and warranty status of this work.
 */

#include "sysinclude.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <fcntl.h>
#include <unistd.h>
#include <termios.h>
#include <sys/stat.h>

#ifdef __GLIBC__
#define _GNU_SOURCE
#include <getopt.h>
#endif

#include "config.h"
#include "kernel/stack.h"
#include "sysinclude.h"
#include "kernel/kernel.h"
#include "dict.h"
#include "cross.h"
#include "openbios-version.h"

#define MAX_PATH_LEN 256

#define MEMORY_SIZE (1024*1024)	/* 1M ram for hosted system */
#define DICTIONARY_SIZE (256*1024) /* 256k for the dictionary   */
#define TRAMPOLINE_SIZE (4*sizeof(cell)) /* 4 cells for the trampoline */

/* state variables */
static ucell *latest, *state, *base;
static ucell *memory;
ucell *trampoline;

/* local variables */
static int errors = 0;
static int segfault = 0;
static int verbose = 0;

#define MAX_SRC_FILES 128

static FILE *srcfiles[MAX_SRC_FILES];
static char *srcfilenames[MAX_SRC_FILES];
static int srclines[MAX_SRC_FILES];
static unsigned int cursrc = 0;

static char *srcbasedict;

/* console variables */
static FILE *console;

#ifdef NATIVE_BITWIDTH_SMALLER_THAN_HOST_BITWIDTH
unsigned long base_address;
#endif

/* include path handling */
typedef struct include_path include;
struct include_path {
        const char *path;
	include *next;
};

static include includes = { ".", NULL };
static FILE *depfile;

static ucell * relocation_address=NULL;
static int     relocation_length=0;

/* the word names are used to generate the prim words in the
 * dictionary. This is done by the C written interpreter.
 */
static const char *wordnames[] = {
	"(semis)", "", "(lit)", "", "", "", "", "(do)", "(?do)", "(loop)",
	"(+loop)", "", "", "", "dup", "2dup", "?dup", "over", "2over", "pick", "drop",
	"2drop", "nip", "roll", "rot", "-rot", "swap", "2swap", ">r", "r>",
	"r@", "depth", "depth!", "rdepth", "rdepth!", "+", "-", "*", "u*",
	"mu/mod", "abs", "negate", "max", "min", "lshift", "rshift", ">>a",
	"and", "or", "xor", "invert", "d+", "d-", "m*", "um*", "@", "c@",
	"w@", "l@", "!", "+!", "c!", "w!", "l!", "=", ">", "<", "u>", "u<",
	"sp@", "move", "fill", "(emit)", "(key?)", "(key)", "execute",
	"here", "here!", "dobranch", "do?branch", "unaligned-w@",
	"unaligned-w!", "unaligned-l@", "unaligned-l!", "ioc@", "iow@",
	"iol@", "ioc!", "iow!", "iol!", "i", "j", "call", "sys-debug",
	"$include", "$encode-file", "(debug", "(debug-off)"
};

/*
 * dictionary related functions.
 */

/*
 * Compare two dictionaries constructed at different addresses. When
 * the cells don't match, a need for relocation is detected and the
 * corresponding bit in reloc_table bitmap is set.
 */
static void relocation_table(unsigned char * dict_one, unsigned char *dict_two, int length)
{
	ucell *d1=(ucell *)dict_one, *d2=(ucell *)dict_two;
	ucell *reloc_table;
	int pos, bit;
	int l=(length+(sizeof(cell)-1))/sizeof(ucell), i;

	/* prepare relocation table */
	relocation_length=(length+BITS-1)/BITS;
	reloc_table = malloc(relocation_length*sizeof(cell));
	memset(reloc_table,0,relocation_length*sizeof(cell));

	for (i=0; i<l; i++) {

		pos=i/BITS;
		bit=i&~(-BITS);

		if(d1[i]==d2[i]) {
                        reloc_table[pos] &= target_ucell(~((ucell)1ULL << bit));

			// This check might bring false positives in data.
			//if(d1[i] >= pointer2cell(dict_one) &&
			//		d1[i] <= pointer2cell(dict_one+length))
			//	printk("\nWARNING: inconsistent relocation (%x:%x)!\n", d1[i], d2[i]);
		} else {
			/* This is a pointer, it needs relocation, d2==dict */
                        reloc_table[pos] |= target_ucell((ucell)1ULL << bit);
			d2[i] = target_ucell(target_ucell(d2[i]) - pointer2cell(d2));
		}
	}

#ifdef CONFIG_DEBUG_DICTIONARY
	printk("dict1 %lx dict2 %lx dict %lx\n",dict_one, dict_two, dict);
	for (i=0; i< relocation_length ; i++)
		printk("reloc %d %lx\n",i+1, reloc_table[i]);
#endif
	relocation_address=reloc_table;
}

static void write_dictionary(const char *filename)
{
	FILE *f;
	unsigned char *write_data, *walk_data;
	int  write_len;
	dictionary_header_t *header;
	u32 checksum=0;

	/*
	 * get memory for dictionary
	 */

	write_len  = sizeof(dictionary_header_t)+dicthead+relocation_length*sizeof(cell);
	write_data = malloc(write_len);
	if(!write_data) {
		printk("panic: can't allocate memory for output dictionary (%d"
			" bytes\n", write_len);
		exit(1);
	}
	memset(write_data, 0, write_len);

	/*
	 * prepare dictionary header
	 */

	header = (dictionary_header_t *)write_data;
	*header = (dictionary_header_t){
		.signature	= DICTID,
		.version	= 2,
		.cellsize	= sizeof(ucell),
#ifdef CONFIG_BIG_ENDIAN
		.endianess	= -1,
#else
		.endianess	= 0,
#endif
		.checksum	= 0,
		.compression	= 0,
		.relocation	= -1,
                .length         = target_ulong((uint32_t)dicthead),
                .last           = target_ucell((ucell)((unsigned long)last
                                                       - (unsigned long)dict)),
	};

	/*
	 * prepare dictionary data
	 */

	walk_data=write_data+sizeof(dictionary_header_t);
	memcpy (walk_data, dict, dicthead);

	/*
	 * prepare relocation data.
	 * relocation_address is zero when writing a dictionary core.
	 */

	if (relocation_address) {
#ifdef CONFIG_DEBUG_DICTIONARY
		printk("writing %d reloc cells \n",relocation_length);
#endif
		walk_data += dicthead;
		memcpy(walk_data, relocation_address,
				relocation_length*sizeof(cell));
		/* free relocation information */
		free(relocation_address);
		relocation_address=NULL;
	} else {
		header->relocation=0;
	}

	/*
	 * Calculate Checksum
	 */

	walk_data=write_data;
	while (walk_data<write_data+write_len) {
		checksum+=read_long(walk_data);
		walk_data+=sizeof(u32);
	}
	checksum=(u32)-checksum;

	header->checksum=target_long(checksum);

        if (verbose) {
                dump_header(header);
        }

	f = fopen(filename, "w");
	if (!f) {
		printk("panic: can't write to dictionary '%s'.\n", filename);
		exit(1);
	}

	fwrite(write_data, write_len, 1, f);

	free(write_data);
	fclose(f);

#ifdef CONFIG_DEBUG_DICTIONARY
	printk("wrote dictionary to file %s.\n", filename);
#endif
}

/*
 * Write dictionary as a list of ucell hex values to filename. Array
 * header and end lines are not generated.
 *
 * Cells with relocations are output using the expression
 * DICTIONARY_BASE + value.
 *
 * Define some helpful constants.
 */
static void write_dictionary_hex(const char *filename)
{
    FILE *f;
    ucell *walk;

    f = fopen(filename, "w");
    if (!f) {
        printk("panic: can't write to dictionary '%s'.\n", filename);
        exit(1);
    }

    for (walk = (ucell *)dict; walk < (ucell *)(dict + dicthead); walk++) {
        int pos, bit, l;
        ucell val;

        l = (walk - (ucell *)dict);
        pos = l / BITS;
        bit = l & ~(-BITS);

        val = read_ucell(walk);
        if (relocation_address[pos] & target_ucell((ucell)1ULL << bit)) {
            fprintf(f, "DICTIONARY_BASE + 0x%" FMT_CELL_x
                    ",\n", val);
        } else {
            fprintf(f, "0x%" FMT_CELL_x",\n", val);
        }
    }

    fprintf(f, "#define FORTH_DICTIONARY_LAST 0x%" FMT_CELL_x"\n",
            (ucell)((unsigned long)last - (unsigned long)dict));
    fprintf(f, "#define FORTH_DICTIONARY_END 0x%" FMT_CELL_x"\n",
            (ucell)dicthead);
    fclose(f);

#ifdef CONFIG_DEBUG_DICTIONARY
    printk("wrote dictionary to file %s.\n", filename);
#endif
}

static ucell read_dictionary(char *fil)
{
	int ilen;
	ucell ret;
	char *mem;
	FILE *f;
	struct stat finfo;

	if (stat(fil, &finfo))
		return 0;

	ilen = finfo.st_size;

	if ((mem = malloc(ilen)) == NULL) {
		printk("panic: not enough memory.\n");
		exit(1);
	}

	f = fopen(fil, "r");
	if (!f) {
		printk("panic: can't open dictionary.\n");
		exit(1);
	}

	if (fread(mem, ilen, 1, f) != 1) {
		printk("panic: can't read dictionary.\n");
		fclose(f);
		exit(1);
	}
	fclose(f);

	ret = load_dictionary(mem, ilen);

	free(mem);
	return ret;
}


/*
 * C Parser related functions
 */

/*
 * skipws skips all whitespaces (space, tab, newline) from the input file
 */

static void skipws(FILE * f)
{
	int c;
	while (!feof(f)) {
		c = getc(f);

		if (c == ' ' || c == '\t')
			continue;

		if (c == '\n') {
			srclines[cursrc - 1]++;
			continue;
		}

		ungetc(c, f);
		break;
	}
}

/*
 * parse gets the next word from the input stream, delimited by
 * delim. If delim is 0, any word delimiter will end the stream
 * word delimiters are space, tab and newline. The resulting word
 * will be put zero delimited to the char array line.
 */

static int parse(FILE * f, char *line, char delim)
{
	int cnt = 0, c = 0;

	while (!feof(f)) {
		c = getc(f);

		if (delim && c == delim)
			break;

		if ((!delim) && (c == ' ' || c == '\t' || c == '\n'))
			break;

		line[cnt++] = c;
	}

	/* Update current line number */
	if (c == '\n') {
		srclines[cursrc - 1]++;
	}

	line[cnt] = 0;

	return cnt;
}

/*
 * parse_word is a small helper that skips whitespaces before a word.
 * it's behaviour is similar to the forth version parse-word.
 */

static void parse_word(FILE * f, char *line)
{
	skipws(f);
	parse(f, line, 0);
}


static void writestring(const char *str)
{
	unsigned int i;
	for (i = 0; i < strlen(str); i++) {
		dict[dicthead + i] = str[i];
	}
	dicthead += i + 1;
	dict[dicthead - 1] = (char) strlen(str) + 128;
}

#define writebyte(value) {write_byte(dict+dicthead,value); dicthead++;}
#define writecell(value) {write_cell(dict+dicthead, value); dicthead+=sizeof(cell);}

/*
 * reveal a word, ie. make it visible.
 */

static void reveal(void)
{
	*last = *latest;
}

/*
 * dictionary padding
 */

static void paddict(ucell align)
{
	while (dicthead % align != 0)
		writebyte(0);
}

/*
 * generic forth word creator function.
 */

static void fcreate(const char *word, ucell cfaval)
{
	if (strlen(word) == 0) {
		printk("WARNING: tried to create unnamed word.\n");
		return;
	}

	writestring(word);
	/* get us at least 1 byte for flags */
	writebyte(0);
	paddict(sizeof(cell));
	/* set flags high bit. */
	dict[dicthead - 1] = 128;
	/* lfa and cfa */
	writecell(read_ucell(latest));
	*latest = target_ucell(pointer2cell(dict) + dicthead - sizeof(cell));
	writecell(cfaval);
}


static ucell *buildvariable(const char *name, cell defval)
{
	fcreate(name, DOVAR);	/* see dict.h for DOVAR and other CFA ids */
	writecell(defval);
	return (ucell *) (dict + dicthead - sizeof(cell));
}

static void buildconstant(const char *name, cell defval)
{
	fcreate(name, DOCON);	/* see dict.h for DOCON and other CFA ids */
	writecell(defval);
}

static void builddefer(const char *name)
{
	fcreate(name, DODFR);	/* see dict.h for DODFR and other CFA ids */
        writecell((ucell)0);
	writecell((ucell)findword("(semis)"));
}

/*
 * Include file handling
 */

static void add_includepath(char *path)
{
	include *incl = &includes;
	include *newpath;

	while (incl->next)
		incl = incl->next;

	newpath = malloc(sizeof(include));
	if (!newpath) {
		printk("panic: not enough memory for include path.\n");
		exit(1);
	}

	incl->next = newpath;
	newpath->path = path;
	newpath->next = NULL;
}


static FILE *fopen_include(const char *fil)
{
	char fullpath[MAX_PATH_LEN];
	FILE *ret;
	include *incl = &includes;

	while (incl) {
                snprintf(fullpath, sizeof(fullpath), "%s/%s", incl->path, fil);

		ret = fopen(fullpath, "r");
		if (ret != NULL) {

#ifdef CONFIG_DEBUG_INTERPRETER
			printk("Including '%s'\n", fil);
#endif
			srcfilenames[cursrc] = strdup(fil);
			srclines[cursrc] = 1;
			srcfiles[cursrc++] = ret;

                        if (depfile) {
                                fprintf(depfile, " %s", fullpath);
                        }

			return ret;
		}

		incl = incl->next;
	}
	return NULL;
}


/*
 * Forth exception handler
 */

void exception(cell no)
{
	printk("%s:%d: ", srcfilenames[cursrc - 1], srclines[cursrc - 1]);

	/* See also forth/bootstrap/interpreter.fs */
	switch (no) {
	case -1:
	case -2:
		printk("Aborted.\n");
		break;
	case -3:
		printk("Stack Overflow.\n");
		break;
	case -4:
		printk("Stack Underflow.\n");
		break;
	case -5:
		printk("Return Stack Overflow.\n");
		break;
	case -6:
		printk("Return Stack Underflow.\n");
		break;
	case -19:
		printk("undefined word.\n");
		break;
	case -21:
		printk("out of memory.\n");
		break;
	case -33:
		printk("undefined method.\n");
		break;
	case -34:
		printk("no such device.\n");
		break;
	default:
		printk("error %" FMT_CELL_d " occured.\n", no);
	}
	exit(1);
}


/*
 * This is the C version of the forth interpreter
 */

static int interpret_source(char *fil)
{
	FILE *f;
	char tib[160];
        cell num;
	char *test;

	const ucell SEMIS = (ucell)findword("(semis)");
	const ucell LIT = (ucell)findword("(lit)");
	const ucell DOBRANCH = (ucell)findword("dobranch");

	if ((f = fopen_include(fil)) == NULL) {
		printk("error while loading source file '%s'\n", fil);
		errors++;
		exit(1);
	}

	/* FIXME: We should read this file at
	 * once. No need to get it char by char
	 */

	while (!feof(f)) {
		xt_t res;
		parse_word(f, tib);

		/* if there is actually no word, we continue right away */
		if (strlen(tib) == 0) {
			continue;
		}

		/* Checking for builtin words that are needed to
		 * bootstrap the forth base dictionary.
		 */

		if (!strcmp(tib, "(")) {
			parse(f, tib, ')');
			continue;
		}

		if (!strcmp(tib, "\\")) {
			parse(f, tib, '\n');
			continue;
		}

		if (!strcmp(tib, ":")) {
			parse_word(f, tib);

#ifdef CONFIG_DEBUG_INTERPRETER
			printk("create colon word %s\n\n", tib);
#endif
			fcreate(tib, DOCOL);	/* see dict.h for DOCOL and other CFA ids */
			*state = (ucell) (-1);
			continue;
		}

		if (!strcmp(tib, ";")) {
#ifdef CONFIG_DEBUG_INTERPRETER
			printk("finish colon definition\n\n");
#endif
			writecell((cell)SEMIS);
			*state = (ucell) 0;
			reveal();
			continue;
		}

		if (!strcasecmp(tib, "variable")) {
			parse_word(f, tib);
#ifdef CONFIG_DEBUG_INTERPRETER
			printk("defining variable %s\n\n", tib);
#endif
			buildvariable(tib, 0);
			reveal();
			continue;
		}

		if (!strcasecmp(tib, "constant")) {
			parse_word(f, tib);
#ifdef CONFIG_DEBUG_INTERPRETER
			printk("defining constant %s\n\n", tib);
#endif
			buildconstant(tib, POP());
			reveal();
			continue;
		}

		if (!strcasecmp(tib, "value")) {
			parse_word(f, tib);
#ifdef CONFIG_DEBUG_INTERPRETER
			printk("defining value %s\n\n", tib);
#endif
			buildconstant(tib, POP());
			reveal();
			continue;
		}

		if (!strcasecmp(tib, "defer")) {
			parse_word(f, tib);
#ifdef CONFIG_DEBUG_INTERPRETER
			printk("defining defer word %s\n\n", tib);
#endif
			builddefer(tib);
			reveal();
			continue;
		}

		if (!strcasecmp(tib, "include")) {
			parse_word(f, tib);
#ifdef CONFIG_DEBUG_INTERPRETER
			printk("including file %s\n\n", tib);
#endif
			interpret_source(tib);
			continue;
		}

		if (!strcmp(tib, "[']")) {
			xt_t xt;
			parse_word(f, tib);
			xt = findword(tib);
			if (*state == 0) {
#ifdef CONFIG_DEBUG_INTERPRETER
				printk
				    ("writing address of %s to stack\n\n",
				     tib);
#endif
				PUSH_xt(xt);
			} else {
#ifdef CONFIG_DEBUG_INTERPRETER
				printk("writing lit, addr(%s) to dict\n\n",
				       tib);
#endif
				writecell(LIT);	/* lit */
				writecell((cell)xt);
			}
			continue;
			/* we have no error detection here */
		}

		if (!strcasecmp(tib, "s\"")) {
			int cnt;
			cell loco;

			cnt = parse(f, tib, '"');
#ifdef CONFIG_DEBUG_INTERPRETER
			printk("compiling string %s\n", tib);
#endif
			loco = dicthead + (6 * sizeof(cell));
			writecell(LIT);
			writecell(pointer2cell(dict) + loco);
			writecell(LIT);
                        writecell((ucell)cnt);
			writecell(DOBRANCH);
			loco = cnt + sizeof(cell) - 1;
			loco &= ~(sizeof(cell) - 1);
			writecell(loco);
			memcpy(dict + dicthead, tib, cnt);
			dicthead += cnt;
			paddict(sizeof(cell));
			continue;
		}

		/* look if tib is in dictionary. */
		/* should the dictionary be searched before the builtins ? */
		res = findword(tib);
		if (res) {
			u8 flags = read_byte((u8*)cell2pointer(res) -
						sizeof(cell) - 1);
#ifdef CONFIG_DEBUG_INTERPRETER
                        printk("%s is 0x%" FMT_CELL_x "\n", tib, (ucell) res);
#endif
			if (!(*state) || (flags & 3)) {
#ifdef CONFIG_DEBUG_INTERPRETER
                                printk("executing %s, %" FMT_CELL_d
                                       " (flags: %s %s)\n",
				       tib, res,
				       (flags & 1) ? "immediate" : "",
				       (flags & 2) ? "compile-only" : "");
#endif
				PC = (ucell)res;
				enterforth(res);
			} else {
#ifdef CONFIG_DEBUG_INTERPRETER
				printk("writing %s to dict\n\n", tib);
#endif
				writecell((cell)res);
			}
			continue;
		}

		/* if not look if it's a number */
		if (tib[0] == '-')
			num = strtoll(tib, &test, read_ucell(base));
		else
			num = strtoull(tib, &test, read_ucell(base));


		if (*test != 0) {
			/* what is it?? */
			printk("%s:%d: %s is not defined.\n\n", srcfilenames[cursrc - 1], srclines[cursrc - 1], tib);
			errors++;
#ifdef CONFIG_DEBUG_INTERPRETER
			continue;
#else
			return -1;
#endif
		}

		if (*state == 0) {
#ifdef CONFIG_DEBUG_INTERPRETER
                        printk("pushed %" FMT_CELL_x " to stack\n\n", num);
#endif
			PUSH(num);
		} else {
#ifdef CONFIG_DEBUG_INTERPRETER
                        printk("writing lit, %" FMT_CELL_x " to dict\n\n", num);
#endif
			writecell(LIT);	/* lit */
			writecell(num);
		}
	}

	fclose(f);
	cursrc--;

	return 0;
}

static int build_dictionary(void)
{
	ucell lfa = 0;
	unsigned int i;

	/* we need a temporary place for latest outside the dictionary */
	latest = &lfa;

	/* starting a new dictionary: clear dicthead */
	dicthead = 0;

#ifdef CONFIG_DEBUG_DICTIONARY
	printk("building dictionary, %d primitives.\nbuilt words:",
	       sizeof(wordnames) / sizeof(void *));
#endif

	for (i = 0; i < sizeof(wordnames) / sizeof(void *); i++) {
		if (strlen(wordnames[i]) != 0) {
			fcreate((char *) wordnames[i], i);
#ifdef CONFIG_DEBUG_DICTIONARY
			printk(" %s", wordnames[i]);
#endif
		}
	}
#ifdef CONFIG_DEBUG_DICTIONARY
	printk(".\n");
#endif

	/* get last/latest and state */
	state = buildvariable("state", 0);
	last = buildvariable("forth-last", 0);
	latest = buildvariable("latest", 0);

	*latest = target_ucell(pointer2cell(latest)-2*sizeof(cell));

	base=buildvariable("base", 10);

	buildconstant("/c", sizeof(u8));
	buildconstant("/w", sizeof(u16));
	buildconstant("/l", sizeof(u32));
	buildconstant("/n", sizeof(ucell));
	buildconstant("/x", sizeof(u64));

	reveal();
        if (verbose) {
                printk("Dictionary initialization finished.\n");
        }
	return 0;
}

/*
 * functions used by primitives
 */

int availchar(void)
{
	int tmp;
	if( cursrc < 1 ) {
		interruptforth |= FORTH_INTSTAT_STOP;
		/* return -1 in order to exit the loop in key() */
		return -1;
	}

	tmp = getc( srcfiles[cursrc-1] );
	if (tmp != EOF) {
		ungetc(tmp, srcfiles[cursrc-1]);
		return -1;
	}

	fclose(srcfiles[--cursrc]);

	return availchar();
}

int get_inputbyte( void )
{
	int tmp;

	if( cursrc < 1 ) {
		interruptforth |= FORTH_INTSTAT_STOP;
		return 0;
	}

	tmp = getc( srcfiles[cursrc-1] );

	/* Update current line number */
	if (tmp == '\n') {
		srclines[cursrc - 1]++;
	}

	if (tmp != EOF) {
		return tmp;
	}

	fclose(srcfiles[--cursrc]);

	return get_inputbyte();
}

void put_outputbyte( int c )
{
	if (console)
		fputc(c, console);
}

/*
 *  segmentation fault handler. linux specific?
 */

static void
segv_handler(int signo __attribute__ ((unused)),
	     siginfo_t * si, void *context __attribute__ ((unused)))
{
	static int count = 0;
	ucell addr = 0xdeadbeef;

	if (count) {
		printk("Died while dumping forth dictionary core.\n");
		goto out;
	}

	count++;

	if (PC >= pointer2cell(dict) && PC <= pointer2cell(dict) + dicthead)
		addr = read_cell(cell2pointer(PC));

	printk("panic: segmentation violation at %p\n", (char *)si->si_addr);
	printk("dict=%p here=%p(dict+0x%" FMT_CELL_x ") pc=0x%" FMT_CELL_x "(dict+0x%" FMT_CELL_x ")\n",
	       dict, dict + dicthead, dicthead, PC, PC - pointer2cell(dict));
	printk("dstackcnt=%d rstackcnt=%d instruction=%" FMT_CELL_x "\n",
	       dstackcnt, rstackcnt, addr);

	printdstack();
	printrstack();

	printk("Writing dictionary core file\n");
	write_dictionary("forth.dict.core");

      out:
	exit(1);
}

/*
 * allocate memory and prepare engine for memory management.
 */

static void init_memory(void)
{
	memset(memory, 0, MEMORY_SIZE);

	/* we push start and end of memory to the stack
	 * so that it can be used by the forth word QUIT
	 * to initialize the memory allocator.
	 * Add a cell to the start address so we don't end
	 * up with a start address of zero during bootstrap
	 */

	PUSH(pointer2cell(memory)+sizeof(cell));
	PUSH(pointer2cell(memory) + MEMORY_SIZE-1);
}


void
include_file( const char *name )
{
	FILE *file;

	if( cursrc >= sizeof(srcfiles)/sizeof(srcfiles[0]) ) {
		printk("\npanic: Maximum include depth reached!\n");
		exit(1);
	}

	file = fopen_include( name );
	if( !file ) {
		printk("\npanic: Failed opening file '%s'\n", name );
		exit(1);
	}
}


void
encode_file( const char *name )
{
	FILE *file = fopen_include(name);
	int size;

	if( !file ) {
		printk("\npanic: Can't open '%s'\n", name );
		exit(1);
	}
	fseek( file, 0, SEEK_END );
	size = ftell( file );
	fseek( file, 0, SEEK_SET );

        if (verbose) {
                printk("\nEncoding %s [%d bytes]\n", name, size );
        }
	fread( dict + dicthead, size, 1, file );
	PUSH( pointer2cell(dict + dicthead) );
	PUSH( size );
	dicthead += size;
	paddict(sizeof(cell));
}


static void run_dictionary(char *basedict, char *confile)
{
	if(!basedict)
		return;

	read_dictionary(basedict);
	PC = (ucell)findword("initialize");

	if (!PC) {
		if (verbose) {
			printk("Unable to find initialize word in dictionary %s; ignoring\n", basedict);
		}
		return;
	}

	if(!srcfiles[0]) {
		cursrc = 1;
		srcfiles[cursrc-1] = stdin;
	}

	dstackcnt=0;
	rstackcnt=0;

	init_memory();
	if (verbose)
		printk("Jumping to dictionary %s...\n", basedict);

	/* If a console file has been specified, open it */
	if (confile)
		console = fopen(confile, "w");

	srcbasedict = basedict;	

	enterforth((xt_t)PC);

	/* Close the console file */
	if (console)
		fclose(console);
}

static void new_dictionary(const char *source)
{
	build_dictionary();

	interpret_source((char *)source);

        if (verbose || errors > 0) {
                printk("interpretion finished. %d errors occured.\n",
                       errors);
        }
}

/*
 * main loop
 */

#define BANNER	"OpenBIOS bootstrap kernel. (C) 2003-2006 Patrick Mauritz, Stefan Reinauer\n"\
		"This software comes with absolutely no warranty. "\
		"All rights reserved.\n\n"

#ifdef __GLIBC__
#define USAGE   "Usage: %s [options] [dictionary file|source file]\n\n" \
		"   -h|--help		show this help\n"		\
		"   -V|--version	print version and exit\n"	\
		"   -v|--verbose        print debugging information\n"	\
		"   -I|--include dir	add dir to include path\n"	\
		"   -d|--source-dictionary bootstrap.dict\n"		\
		"			use this dictionary as base\n"	\
		"   -D|--target-dictionary output.dict\n"		\
		"			write to output.dict\n"		\
		"   -c|--console output.log\n"		\
		"			write kernel console output to log file\n"	\
		"   -s|--segfault	install segfault handler\n"     \
                "   -M|--dependency-dump file\n"                         \
                "                       dump dependencies in Makefile format\n\n" \
                "   -x|--hexdump        output format is C language hex dump\n"
#else
#define USAGE   "Usage: %s [options] [dictionary file|source file]\n\n" \
		"   -h		show this help\n"		\
		"   -V		print version and exit\n"	\
		"   -v		print debugging information\n"	\
		"   -I		add dir to include path\n"	\
		"   -d bootstrap.dict\n"			\
		"		use this dictionary as base\n"	\
		"   -D output.dict\n"				\
		"		write to output.dict\n"		\
		"   -c output.log\n"		\
		"		write kernel console output to log file\n"	\
		"   -s		install segfault handler\n\n"   \
                "   -M file     dump dependencies in Makefile format\n\n" \
                "   -x          output format is C language hex dump\n"
#endif

int main(int argc, char *argv[])
{
	struct sigaction sa;

	unsigned char *ressources=NULL; /* All memory used by us */
        const char *dictname = NULL;
	char *basedict = NULL;
	char *consolefile = NULL;
        char *depfilename = NULL;

	unsigned char *bootstrapdict[2];
        int c, cnt, hexdump = 0;

        const char *optstring = "VvhsI:d:D:c:M:x?";

	while (1) {
#ifdef __GLIBC__
		int option_index = 0;
		static struct option long_options[] = {
			{"version", 0, NULL, 'V'},
			{"verbose", 0, NULL, 'v'},
			{"help", 0, NULL, 'h'},
			{"segfault", 0, NULL, 's'},
			{"include", 1, NULL, 'I'},
			{"source-dictionary", 1, NULL, 'd'},
			{"target-dictionary", 1, NULL, 'D'},
			{"console", 1, NULL, 'c'},
                        {"dependency-dump", 1, NULL, 'M'},
                        {"hexdump", 0, NULL, 'x'},
		};

		/*
		 * option handling
		 */

		c = getopt_long(argc, argv, optstring, long_options,
				&option_index);
#else
		c = getopt(argc, argv, optstring);
#endif
		if (c == -1)
			break;

		switch (c) {
		case 'V':
                        printk("Version " OPENBIOS_VERSION_STR "\n");
			return 0;
		case 'h':
		case '?':
                        printk("Version " OPENBIOS_VERSION_STR "\n" USAGE,
                               argv[0]);
			return 0;
		case 'v':
			verbose = 1;
			break;
		case 's':
			segfault = 1;
			break;
		case 'I':
#ifdef CONFIG_DEBUG_INTERPRETER
			printk("adding '%s' to include path\n", optarg);
#endif
			add_includepath(optarg);
			break;
		case 'd':
			if (!basedict) {
				basedict = optarg;
			}
			break;
		case 'D':
			if(!dictname) {
				dictname = optarg;
			}
			break;
		case 'c':
			if (!consolefile) {
				consolefile = optarg;
			}
			break;
                case 'M':
                        if (!depfilename) {
                                depfilename = optarg;
                        }
                        break;
                case 'x':
                        hexdump = 1;
                        break;
		default:
			return 1;
		}
	}

        if (!dictname) {
            dictname = "bootstrap.dict";
        }
        if (verbose) {
                printk(BANNER);
                printk("Using source dictionary '%s'\n", basedict);
                printk("Dumping final dictionary to '%s'\n", dictname);
                printk("Dumping dependencies to '%s'\n", depfilename);
        }

        if (argc < optind) {
		printk(USAGE, argv[0]);
		return 1;
	}

        if (depfilename) {
            depfile = fopen(depfilename, "w");
            if (!depfile) {
                printk("panic: can't write to dependency file '%s'.\n",
                       depfilename);
                exit(1);
            }
            fprintf(depfile, "%s:", dictname);
        }

	/*
	 * Get all required resources
	 */


	ressources = malloc(MEMORY_SIZE + (2 * DICTIONARY_SIZE) + TRAMPOLINE_SIZE);
	if (!ressources) {
		printk("panic: not enough memory on host system.\n");
		return 1;
	}

#ifdef NATIVE_BITWIDTH_SMALLER_THAN_HOST_BITWIDTH
	base_address=(unsigned long)ressources;
#endif

	memory = (ucell *)ressources;

	bootstrapdict[0] = ressources + MEMORY_SIZE;
	bootstrapdict[1] = ressources + MEMORY_SIZE + DICTIONARY_SIZE;
	trampoline = (ucell *)(ressources + MEMORY_SIZE + DICTIONARY_SIZE + DICTIONARY_SIZE);

#ifdef CONFIG_DEBUG_INTERPRETER
	printf("memory: %p\n",memory);
	printf("dict1: %p\n",bootstrapdict[0]);
	printf("dict2: %p\n",bootstrapdict[1]);
	printf("trampoline: %p\n",trampoline);
	printf("size=%d, trampoline_size=%d\n",MEMORY_SIZE + (2 *
				DICTIONARY_SIZE) + TRAMPOLINE_SIZE,
			TRAMPOLINE_SIZE);
#endif

	if (trampoline == NULL) {
		/* We're using side effects which is to some extent nasty */
		printf("WARNING: no trampoline!\n");
	} else {
		init_trampoline(trampoline);
	}

	if (!segfault) {
		if (verbose)
			printk("Installing SIGSEGV handler...");

		sa.sa_sigaction = segv_handler;
		sigemptyset(&sa.sa_mask);
		sa.sa_flags = SA_SIGINFO | SA_NODEFER;
                sigaction(SIGSEGV, &sa, NULL);

		if (verbose)
			printk("done.\n");
	}

	/*
	 * Now do the real work
	 */

	for (cnt=0; cnt<2; cnt++) {
                if (verbose) {
                        printk("Compiling dictionary %d/%d\n", cnt+1, 2);
                }
		dict=bootstrapdict[cnt];
		if(!basedict) {
			new_dictionary(argv[optind]);
		} else {
			for (c=argc-1; c>=optind; c--)
				include_file(argv[c]);

			run_dictionary(basedict, consolefile);
		}
                if (depfile) {
                        fprintf(depfile, "\n");
                        fclose(depfile);
                        depfile = NULL;
                }
		if(errors)
			break;
	}

#ifndef CONFIG_DEBUG_INTERPRETER
	if (errors)
		printk("dictionary not dumped to file.\n");
	else
#endif
	{
		relocation_table( bootstrapdict[0], bootstrapdict[1], dicthead);
                if (hexdump) {
                    write_dictionary_hex(dictname);
                } else {
                    write_dictionary(dictname);
                }
	}

	free(ressources);

        if (errors)
            return 1;
        else
            return 0;
}