Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 196 lines (160 sloc) 4.566 kb
fa99696 @nimblemachines added $ tags to files in main directory
nimblemachines authored
1 /*
2 * $Id$
3 *
4 * This file is part of muforth.
5 *
8162f89 @nimblemachines Initial path at making muforth an indirect-threaded forth. It now boots,
nimblemachines authored
6 * Copyright (c) 1997-2005 David Frech. All rights reserved, and all wrongs
fa99696 @nimblemachines added $ tags to files in main directory
nimblemachines authored
7 * reversed.
c506d6c @nimblemachines Lots of changes. Now every file starts with an Id, a copyright message,
nimblemachines authored
8 *
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
12 *
13 * http://www.apache.org/licenses/LICENSE-2.0
14 *
15 * or see the file LICENSE in the top directory of this distribution.
16 *
17 * Unless required by applicable law or agreed to in writing, software
18 * distributed under the License is distributed on an "AS IS" BASIS,
19 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20 * See the License for the specific language governing permissions and
21 * limitations under the License.
fa99696 @nimblemachines added $ tags to files in main directory
nimblemachines authored
22 */
23
61f6787 @nimblemachines Initial revision
nimblemachines authored
24 #include "muforth.h"
7900831 @nimblemachines I had to do a sick hack to make counted strings work.
nimblemachines authored
25 #include "version.h"
61f6787 @nimblemachines Initial revision
nimblemachines authored
26
27 #include <sys/mman.h>
28 #include <stdio.h>
29 #include <ctype.h>
f105bc4 @rthelen Added floating point cell type. This is to support native floating poin...
rthelen authored
30 #include <stdlib.h>
61f6787 @nimblemachines Initial revision
nimblemachines authored
31
32 /* data stack */
8162f89 @nimblemachines Initial path at making muforth an indirect-threaded forth. It now boots,
nimblemachines authored
33 cell stack[STACK_SIZE];
97cd764 @nimblemachines Working finally!
nimblemachines authored
34 cell *SP;
61f6787 @nimblemachines Initial revision
nimblemachines authored
35
30e571c @rthelen A new compiler is checked in. This version uses an abstract opcode
rthelen authored
36 /* return stack */
8162f89 @nimblemachines Initial path at making muforth an indirect-threaded forth. It now boots,
nimblemachines authored
37 xtk *rstack[STACK_SIZE];
97cd764 @nimblemachines Working finally!
nimblemachines authored
38 xtk **RP;
30e571c @rthelen A new compiler is checked in. This version uses an abstract opcode
rthelen authored
39
8162f89 @nimblemachines Initial path at making muforth an indirect-threaded forth. It now boots,
nimblemachines authored
40 cell T; /* top of stack */
41 xtk *IP; /* instruction pointer */
42 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
43
61f6787 @nimblemachines Initial revision
nimblemachines authored
44 int cmd_line_argc;
45 char **cmd_line_argv;
46
8162f89 @nimblemachines Initial path at making muforth an indirect-threaded forth. It now boots,
nimblemachines authored
47 uint8 *pdt0; /* ptrs to name & data spaces */
48 cell *pcd0; /* ptr to names & code space */
7dea8a7 @nimblemachines Added code_t for each arch.
nimblemachines authored
49
8162f89 @nimblemachines Initial path at making muforth an indirect-threaded forth. It now boots,
nimblemachines authored
50 uint8 *pdt; /* ptrs to next free byte in each space */
51 cell *pcd;
61f6787 @nimblemachines Initial revision
nimblemachines authored
52
7900831 @nimblemachines I had to do a sick hack to make counted strings work.
nimblemachines authored
53 /* XXX: Gross hack alert! */
54 char *ate_the_stack;
eecd8c3 @rthelen I'm adding support for a second set of stacks: dbg_stack and dbg_rstack.
rthelen authored
55 char *ate_the_rstack;
7900831 @nimblemachines I had to do a sick hack to make counted strings work.
nimblemachines authored
56 char *isnt_defined;
57 char *version;
61f6787 @nimblemachines Initial revision
nimblemachines authored
58
97cd764 @nimblemachines Working finally!
nimblemachines authored
59 static void init_stacks()
60 {
61 mu_sp_reset();
62 RP = R0;
63 }
64
aa2db21 @nimblemachines Final checkin to bring the repo up-to-date with what I've been doing
nimblemachines authored
65 static void mu_find_init_file()
61f6787 @nimblemachines Initial revision
nimblemachines authored
66 {
150bd76 @nimblemachines Fixed all uses of C-style strings that could possibly call into Forth co...
nimblemachines authored
67 /* these don't have to be proper counted strings because they are
68 * used only by C code hereafter.
69 */
61f6787 @nimblemachines Initial revision
nimblemachines authored
70 PUSH("startup.mu4");
aa2db21 @nimblemachines Final checkin to bring the repo up-to-date with what I've been doing
nimblemachines authored
71 mu_readable_q();
72 if (POP) return;
73
74 PUSH("/usr/local/share/muforth/startup.mu4");
75 mu_readable_q();
76 if (POP) return;
77
78 die("couldn't find startup.mu4 init file");
79 }
80
81 void mu_push_code_size()
82 {
7dea8a7 @nimblemachines Added code_t for each arch.
nimblemachines authored
83 PUSH((caddr_t)pcd - (caddr_t)pcd0);
aa2db21 @nimblemachines Final checkin to bring the repo up-to-date with what I've been doing
nimblemachines authored
84 }
85
86 void mu_push_data_size()
87 {
88 PUSH(pdt - pdt0);
61f6787 @nimblemachines Initial revision
nimblemachines authored
89 }
90
aa2db21 @nimblemachines Final checkin to bring the repo up-to-date with what I've been doing
nimblemachines authored
91 static void allocate()
61f6787 @nimblemachines Initial revision
nimblemachines authored
92 {
8162f89 @nimblemachines Initial path at making muforth an indirect-threaded forth. It now boots,
nimblemachines authored
93 pcd0 = (cell *) mmap(0, 256 * 4096, PROT_READ | PROT_WRITE,
94 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
95
8162f89 @nimblemachines Initial path at making muforth an indirect-threaded forth. It now boots,
nimblemachines authored
96 pdt0 = (uint8 *) mmap(0, 1024 * 4096, PROT_READ | PROT_WRITE,
97 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
98
8162f89 @nimblemachines Initial path at making muforth an indirect-threaded forth. It now boots,
nimblemachines authored
99 if (pcd0 == MAP_FAILED || pdt0 == MAP_FAILED)
100 die("couldn't allocate memory");
61f6787 @nimblemachines Initial revision
nimblemachines authored
101
102 /* init compiler ptrs */
103 pcd = pcd0;
104 pdt = pdt0;
105 }
106
150bd76 @nimblemachines Fixed all uses of C-style strings that could possibly call into Forth co...
nimblemachines authored
107 /*
7900831 @nimblemachines I had to do a sick hack to make counted strings work.
nimblemachines authored
108 * This is a horrendous hack. gcc 3.3 is smart enough to let me do what
109 * I want using initializers, but 2.95 complains. So I have to run a bit
110 * of code that compiles some strings into the dictionary, and sets a
111 * few globals to point to them. It's really ugly.
112 */
113 static void make_constant_strings()
114 {
115 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
116 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
117 isnt_defined = to_counted_string("isn't defined");
118 version = to_counted_string(VERSION);
119 }
120
121 /*
8162f89 @nimblemachines Initial path at making muforth an indirect-threaded forth. It now boots,
nimblemachines authored
122 ( 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
123 inefficiency of C's way of doing cmd line params - and parse that.)
124
8162f89 @nimblemachines Initial path at making muforth an indirect-threaded forth. It now boots,
nimblemachines authored
125 ( copy with trailing blank, but don't include the blank in the new addr)
126 : "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
127
8162f89 @nimblemachines Initial path at making muforth an indirect-threaded forth. It now boots,
nimblemachines authored
128 : command-line ( skip first one: the program name)
129 ram argv cell+ ram argc 1- ?for
130 over @ dup string-length "copy 1+ ( keep blank) cell u+ next then
131 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
132 */
133
134 static struct counted_string *pcmd_line;
135
136 static char *copy_string(char *to, char *from, size_t length)
61f6787 @nimblemachines Initial revision
nimblemachines authored
137 {
150bd76 @nimblemachines Fixed all uses of C-style strings that could possibly call into Forth co...
nimblemachines authored
138 memcpy(to, from, length);
139 to += length;
140 *to++ = ' ';
141 return to;
142 }
61f6787 @nimblemachines Initial revision
nimblemachines authored
143
150bd76 @nimblemachines Fixed all uses of C-style strings that could possibly call into Forth co...
nimblemachines authored
144 static void convert_command_line(int argc, char *argv[])
145 {
146 char *pline;
147
148 /* skip arg[0] */
149 argc--;
150 argv++;
151
152 pcmd_line = (struct counted_string *)pdt;
153 pline = pcmd_line->data;
154
155 while (argc--)
156 {
8162f89 @nimblemachines Initial path at making muforth an indirect-threaded forth. It now boots,
nimblemachines authored
157 pline = copy_string(pline, *argv, strlen(*argv));
158 argv++;
150bd76 @nimblemachines Fixed all uses of C-style strings that could possibly call into Forth co...
nimblemachines authored
159 }
7900831 @nimblemachines I had to do a sick hack to make counted strings work.
nimblemachines authored
160 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
161
162 /* null terminate and align */
163 pdt = pline;
164 *pdt++ = 0;
5baa65a @nimblemachines Added cell_t and double_cell_t for both arch's.
nimblemachines authored
165 pdt = (uint8_t *)ALIGNED(pdt);
150bd76 @nimblemachines Fixed all uses of C-style strings that could possibly call into Forth co...
nimblemachines authored
166 }
167
168 void mu_push_command_line()
169 {
170 PUSH(&pcmd_line->data);
171 }
172
7900831 @nimblemachines I had to do a sick hack to make counted strings work.
nimblemachines authored
173 void mu_push_version()
174 {
175 PUSH(version);
176 }
177
178 void mu_push_build_time()
179 {
180 PUSH(build_time);
181 }
182
150bd76 @nimblemachines Fixed all uses of C-style strings that could possibly call into Forth co...
nimblemachines authored
183 int main(int argc, char *argv[])
184 {
61f6787 @nimblemachines Initial revision
nimblemachines authored
185 allocate();
186 init_dict();
150bd76 @nimblemachines Fixed all uses of C-style strings that could possibly call into Forth co...
nimblemachines authored
187 convert_command_line(argc, argv);
8162f89 @nimblemachines Initial path at making muforth an indirect-threaded forth. It now boots,
nimblemachines authored
188 make_constant_strings(); /* XXX: Hack! */
97cd764 @nimblemachines Working finally!
nimblemachines authored
189 init_stacks();
aa2db21 @nimblemachines Final checkin to bring the repo up-to-date with what I've been doing
nimblemachines authored
190 mu_find_init_file();
191 mu_load_file();
192 mu_start_up();
61f6787 @nimblemachines Initial revision
nimblemachines authored
193 return 0;
194 }
195
Something went wrong with that request. Please try again.