Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 191 lines (151 sloc) 4.206 kb
fa99696 @nimblemachines added $ tags to files in main directory
nimblemachines authored
1 /*
10ee5ce @nimblemachines New copyright, new license, new homepage link
nimblemachines authored
2 * This file is part of muFORTH: http://pages.nimblemachines.com/muforth
fa99696 @nimblemachines added $ tags to files in main directory
nimblemachines authored
3 *
10ee5ce @nimblemachines New copyright, new license, new homepage link
nimblemachines authored
4 * Copyright (c) 1997-2008 David Frech. All rights reserved, and all wrongs
5 * reversed. (See the file COPYRIGHT for details.)
fa99696 @nimblemachines added $ tags to files in main directory
nimblemachines authored
6 */
7
61f6787 @nimblemachines Initial revision
nimblemachines authored
8 #include "muforth.h"
7900831 @nimblemachines I had to do a sick hack to make counted strings work.
nimblemachines authored
9 #include "version.h"
61f6787 @nimblemachines Initial revision
nimblemachines authored
10
15acac0 @nimblemachines Moved mu_start_up to muforth.c, and made it static, so it doesn't show u...
nimblemachines authored
11 #include <stdlib.h>
61f6787 @nimblemachines Initial revision
nimblemachines authored
12 #include <sys/mman.h>
13
14 /* data stack */
8162f89 @nimblemachines Initial path at making muforth an indirect-threaded forth. It now boots,
nimblemachines authored
15 cell stack[STACK_SIZE];
97cd764 @nimblemachines Working finally!
nimblemachines authored
16 cell *SP;
61f6787 @nimblemachines Initial revision
nimblemachines authored
17
30e571c @rthelen A new compiler is checked in. This version uses an abstract opcode
rthelen authored
18 /* return stack */
8162f89 @nimblemachines Initial path at making muforth an indirect-threaded forth. It now boots,
nimblemachines authored
19 xtk *rstack[STACK_SIZE];
97cd764 @nimblemachines Working finally!
nimblemachines authored
20 xtk **RP;
30e571c @rthelen A new compiler is checked in. This version uses an abstract opcode
rthelen authored
21
3244585 @nimblemachines Made stack names better.
nimblemachines authored
22 cell TOP; /* top of stack */
8162f89 @nimblemachines Initial path at making muforth an indirect-threaded forth. It now boots,
nimblemachines authored
23 xtk *IP; /* instruction pointer */
24 xtk W; /* on entry, points to the current Forth word */
30e571c @rthelen A new compiler is checked in. This version uses an abstract opcode
rthelen authored
25
61f6787 @nimblemachines Initial revision
nimblemachines authored
26 int cmd_line_argc;
27 char **cmd_line_argv;
28
2e5e46b @nimblemachines count of bytes "allotted" to names, separate from code
nimblemachines authored
29 int names_size; /* count of bytes alloted to names */
30
1459c9a @nimblemachines moved dict from data space (WRONG!!) into code space. now I don't have a...
nimblemachines authored
31 cell *pcd0; /* pointer to start of code & names space */
5392099 @nimblemachines Moved names to data space, simplified dict_entry (more like how it was w...
nimblemachines authored
32 uint8 *pdt0; /* ... data space */
33
1459c9a @nimblemachines moved dict from data space (WRONG!!) into code space. now I don't have a...
nimblemachines authored
34 cell *pcd; /* ptrs to next free byte in code & names space */
5392099 @nimblemachines Moved names to data space, simplified dict_entry (more like how it was w...
nimblemachines authored
35 uint8 *pdt; /* ... data space */
7dea8a7 @nimblemachines Added code_t for each arch.
nimblemachines authored
36
61f6787 @nimblemachines Initial revision
nimblemachines authored
37
7900831 @nimblemachines I had to do a sick hack to make counted strings work.
nimblemachines authored
38 /* XXX: Gross hack alert! */
39 char *ate_the_stack;
eecd8c3 @rthelen I'm adding support for a second set of stacks: dbg_stack and dbg_rstack.
rthelen authored
40 char *ate_the_rstack;
7900831 @nimblemachines I had to do a sick hack to make counted strings work.
nimblemachines authored
41 char *isnt_defined;
61f6787 @nimblemachines Initial revision
nimblemachines authored
42
97cd764 @nimblemachines Working finally!
nimblemachines authored
43 static void init_stacks()
44 {
45 mu_sp_reset();
46 RP = R0;
47 }
48
aa2db21 @nimblemachines Final checkin to bring the repo up-to-date with what I've been doing
nimblemachines authored
49 static void mu_find_init_file()
61f6787 @nimblemachines Initial revision
nimblemachines authored
50 {
5392099 @nimblemachines Moved names to data space, simplified dict_entry (more like how it was w...
nimblemachines authored
51 /* These don't have to be proper counted strings because they are
150bd76 @nimblemachines Fixed all uses of C-style strings that could possibly call into Forth co...
nimblemachines authored
52 * used only by C code hereafter.
53 */
61f6787 @nimblemachines Initial revision
nimblemachines authored
54 PUSH("startup.mu4");
aa2db21 @nimblemachines Final checkin to bring the repo up-to-date with what I've been doing
nimblemachines authored
55 mu_readable_q();
56 if (POP) return;
57
58 PUSH("/usr/local/share/muforth/startup.mu4");
59 mu_readable_q();
60 if (POP) return;
61
62 die("couldn't find startup.mu4 init file");
63 }
64
65 void mu_push_code_size()
66 {
2e5e46b @nimblemachines count of bytes "allotted" to names, separate from code
nimblemachines authored
67 PUSH(((caddr_t)pcd - (caddr_t)pcd0) - names_size);
68 }
69
70 void mu_push_names_size()
71 {
72 PUSH(names_size);
aa2db21 @nimblemachines Final checkin to bring the repo up-to-date with what I've been doing
nimblemachines authored
73 }
74
75 void mu_push_data_size()
76 {
77 PUSH(pdt - pdt0);
61f6787 @nimblemachines Initial revision
nimblemachines authored
78 }
79
aa2db21 @nimblemachines Final checkin to bring the repo up-to-date with what I've been doing
nimblemachines authored
80 static void allocate()
61f6787 @nimblemachines Initial revision
nimblemachines authored
81 {
8162f89 @nimblemachines Initial path at making muforth an indirect-threaded forth. It now boots,
nimblemachines authored
82 pcd0 = (cell *) mmap(0, 256 * 4096, PROT_READ | PROT_WRITE,
83 MAP_ANON | MAP_PRIVATE, -1, 0);
aa2db21 @nimblemachines Final checkin to bring the repo up-to-date with what I've been doing
nimblemachines authored
84
8162f89 @nimblemachines Initial path at making muforth an indirect-threaded forth. It now boots,
nimblemachines authored
85 pdt0 = (uint8 *) mmap(0, 1024 * 4096, PROT_READ | PROT_WRITE,
86 MAP_ANON | MAP_PRIVATE, -1, 0);
aa2db21 @nimblemachines Final checkin to bring the repo up-to-date with what I've been doing
nimblemachines authored
87
8162f89 @nimblemachines Initial path at making muforth an indirect-threaded forth. It now boots,
nimblemachines authored
88 if (pcd0 == MAP_FAILED || pdt0 == MAP_FAILED)
89 die("couldn't allocate memory");
61f6787 @nimblemachines Initial revision
nimblemachines authored
90
91 /* init compiler ptrs */
92 pcd = pcd0;
93 pdt = pdt0;
94 }
95
150bd76 @nimblemachines Fixed all uses of C-style strings that could possibly call into Forth co...
nimblemachines authored
96 /*
7900831 @nimblemachines I had to do a sick hack to make counted strings work.
nimblemachines authored
97 * This is a horrendous hack. gcc 3.3 is smart enough to let me do what
98 * I want using initializers, but 2.95 complains. So I have to run a bit
99 * of code that compiles some strings into the dictionary, and sets a
100 * few globals to point to them. It's really ugly.
101 */
102 static void make_constant_strings()
103 {
104 ate_the_stack = to_counted_string("ate the stack");
eecd8c3 @rthelen I'm adding support for a second set of stacks: dbg_stack and dbg_rstack.
rthelen authored
105 ate_the_rstack = to_counted_string("ate the return stack");
7900831 @nimblemachines I had to do a sick hack to make counted strings work.
nimblemachines authored
106 isnt_defined = to_counted_string("isn't defined");
107 }
108
109 /*
8162f89 @nimblemachines Initial path at making muforth an indirect-threaded forth. It now boots,
nimblemachines authored
110 ( We need to rebuild the command line - says something about the
150bd76 @nimblemachines Fixed all uses of C-style strings that could possibly call into Forth co...
nimblemachines authored
111 inefficiency of C's way of doing cmd line params - and parse that.)
112
8162f89 @nimblemachines Initial path at making muforth an indirect-threaded forth. It now boots,
nimblemachines authored
113 ( copy with trailing blank, but don't include the blank in the new addr)
114 : "copy ( to from u - to+u) push over r@ cmove pop + bl over c! ;
150bd76 @nimblemachines Fixed all uses of C-style strings that could possibly call into Forth co...
nimblemachines authored
115
8162f89 @nimblemachines Initial path at making muforth an indirect-threaded forth. It now boots,
nimblemachines authored
116 : command-line ( skip first one: the program name)
117 ram argv cell+ ram argc 1- ?for
118 over @ dup string-length "copy 1+ ( keep blank) cell u+ next then
119 nip ( start end) over - ( a u) dup allot ( aligns) ;
150bd76 @nimblemachines Fixed all uses of C-style strings that could possibly call into Forth co...
nimblemachines authored
120 */
121
122 static struct counted_string *pcmd_line;
123
124 static char *copy_string(char *to, char *from, size_t length)
61f6787 @nimblemachines Initial revision
nimblemachines authored
125 {
733bfd3 @nimblemachines Minor cleanups so I could get my old arm tools working. Switched from us...
nimblemachines authored
126 bcopy(from, to, length);
150bd76 @nimblemachines Fixed all uses of C-style strings that could possibly call into Forth co...
nimblemachines authored
127 to += length;
128 *to++ = ' ';
129 return to;
130 }
61f6787 @nimblemachines Initial revision
nimblemachines authored
131
150bd76 @nimblemachines Fixed all uses of C-style strings that could possibly call into Forth co...
nimblemachines authored
132 static void convert_command_line(int argc, char *argv[])
133 {
134 char *pline;
135
136 /* skip arg[0] */
137 argc--;
138 argv++;
139
140 pcmd_line = (struct counted_string *)pdt;
141 pline = pcmd_line->data;
142
143 while (argc--)
144 {
8162f89 @nimblemachines Initial path at making muforth an indirect-threaded forth. It now boots,
nimblemachines authored
145 pline = copy_string(pline, *argv, strlen(*argv));
146 argv++;
150bd76 @nimblemachines Fixed all uses of C-style strings that could possibly call into Forth co...
nimblemachines authored
147 }
7900831 @nimblemachines I had to do a sick hack to make counted strings work.
nimblemachines authored
148 pcmd_line->length = pline - pcmd_line->data;
150bd76 @nimblemachines Fixed all uses of C-style strings that could possibly call into Forth co...
nimblemachines authored
149
150 /* null terminate and align */
bf315ef @nimblemachines Silence a pesky warning by dangerously casting about!
nimblemachines authored
151 pdt = (uint8 *)pline;
150bd76 @nimblemachines Fixed all uses of C-style strings that could possibly call into Forth co...
nimblemachines authored
152 *pdt++ = 0;
df0cf33 Fixed another use of uint8_t and removed unused int8 typedef.
Michael Pruemm authored
153 pdt = (uint8 *)ALIGNED(pdt);
150bd76 @nimblemachines Fixed all uses of C-style strings that could possibly call into Forth co...
nimblemachines authored
154 }
155
156 void mu_push_command_line()
157 {
158 PUSH(&pcmd_line->data);
159 }
160
7900831 @nimblemachines I had to do a sick hack to make counted strings work.
nimblemachines authored
161 void mu_push_build_time()
162 {
163 PUSH(build_time);
164 }
165
15acac0 @nimblemachines Moved mu_start_up to muforth.c, and made it static, so it doesn't show u...
nimblemachines authored
166 static void mu_start_up()
167 {
168 PUSH("warm"); /* push the token "warm" */
169 PUSH(4);
170 _mu__lbracket(); /* ... and execute it! */
171 }
172
173 void mu_bye()
174 {
175 exit(0);
176 }
177
150bd76 @nimblemachines Fixed all uses of C-style strings that could possibly call into Forth co...
nimblemachines authored
178 int main(int argc, char *argv[])
179 {
61f6787 @nimblemachines Initial revision
nimblemachines authored
180 allocate();
181 init_dict();
150bd76 @nimblemachines Fixed all uses of C-style strings that could possibly call into Forth co...
nimblemachines authored
182 convert_command_line(argc, argv);
8162f89 @nimblemachines Initial path at making muforth an indirect-threaded forth. It now boots,
nimblemachines authored
183 make_constant_strings(); /* XXX: Hack! */
97cd764 @nimblemachines Working finally!
nimblemachines authored
184 init_stacks();
aa2db21 @nimblemachines Final checkin to bring the repo up-to-date with what I've been doing
nimblemachines authored
185 mu_find_init_file();
186 mu_load_file();
187 mu_start_up();
61f6787 @nimblemachines Initial revision
nimblemachines authored
188 return 0;
189 }
190
Something went wrong with that request. Please try again.