Merge tag 'upstream/0.2.1'
[debian/yforth] / yfinit.c
1 /* yForth? - A Forth interpreter written in ANSI C
2  * Copyright (C) 2012 Luca Padovani
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  * ------------------------------------------------------------------------
17  * Module name:     yfinit.c
18  * Abstract:        Allocate memory for the main structures of the
19  *                  environment and initialize the environment itself.
20  */
21
22 #include <stdlib.h>
23 #include <malloc.h>
24 #include <stdio.h>
25 #include "yforth.h"
26 #include "core.h"
27 #include "coree.h"
28 #include "search.h"
29
30 /* init_stacks: allocate memory for the stacks */
31 void init_stacks(int dstack_size, int rstack_size, int fstack_size) {
32         sp_base = (Cell *) malloc(dstack_size * sizeof(Cell));
33         rp_base = (Cell *) malloc(rstack_size * sizeof(Cell));
34         fp_base = (Real *) malloc(fstack_size * sizeof(Real));
35     if (sp_base && rp_base && (fp_base || !fstack_size)) {
36                 sp = sp_top = sp_base + dstack_size;
37                 rp = rp_top = rp_base + rstack_size;
38                 fp = fp_top = fp_base + fstack_size;
39         } else {
40         fprintf(stderr, "Stack sizes: %d %d %d. Not enough memory.\n", dstack_size,
41                         rstack_size, fstack_size);
42                 exit(-1);
43         }
44 }
45
46 /* init_data_space: allocate memory for the data-space dictionary */
47 void init_data_space(int dspace_size) {
48         dp0 = _dp = (Char *) malloc(dspace_size * sizeof(Cell));
49         if (!_dp) {
50                 printf("Data Space size: %d. Not enough memory.\n", dspace_size);
51                 exit(-1);
52         }
53 }
54
55 /* init_tib: allocate memory for the TIB */
56 void init_tib(int size) {
57         _tib = (Char *) malloc(size * sizeof(Char));
58         if (!_tib) {
59         fprintf(stderr, "Tib size: %d. Not enough memory.\n", size);
60                 exit(-1);
61         }
62 }
63
64 /* init_pad: allocate memory for the PAD */
65 void init_pad(int size) {
66         _pad = (Char *) malloc(size * sizeof(Char));
67         if (!_pad) {
68         fprintf(stderr, "PAD size: %d. Not enough memory.\n", size);
69                 exit(-1);
70         }
71 }
72
73 /* init_pnos: allocate memory for PNOS, note that the size of PNOS is
74  * determined by the actual size of a double cell.
75  */
76 void init_pnos() {
77     pnos_size = sizeof(DCell) * 8 + 2;      /* plus a space and eventually a '-' */
78         pnos = (Char *) malloc(pnos_size * sizeof(Char));
79         if (!pnos) {
80         fprintf(stderr, "Can't allocate PNOS.\n");
81                 exit(-1);
82         }
83 }
84
85 /* init_forth_environment: perform actual inizialization of the dictionary
86  * only if "reload" is true, then initialize the value of variables
87  * of the forth environment. This variable must be initialized even in
88  * the case of an image file since they're not inside the dictionary,
89  * but are simply C variables.
90  */
91 void init_forth_environment(int reload) {
92         if (reload) {
93                 struct vocabulary *v;
94                 _wordlist();
95                 list[0] = forth_wid = voc = (struct vocabulary *) *sp++;
96                 _last = NULL;
97                 init_vocabulary(&_dp);
98         }
99         _base = 10;
100         _env_slash_counted_string = (1 << (8 * sizeof(Char))) - 1;
101         _env_slash_hold =               pnos_size;
102         _env_slash_pad =                pad_size;
103         _env_address_unit_bits = 8 * sizeof(Char);
104         _env_core =                     FFLAG(1);
105         _env_core_ext =         FFLAG(COREE_DEF);
106         _env_floored =                  FFLAG(FLOORED_DIVISION);
107         _env_max_char =                 _env_slash_counted_string;
108         _env_max_d =                    MAX_D;
109         _env_max_n =                    MAX_N;
110         _env_max_u =                    MAX_U;
111         _env_max_ud =                   MAX_UD;
112         _env_return_stack_cells = rstack_size;
113         _env_stack_cells =              dstack_size;
114         _env_double =                   FFLAG(DOUBLE_DEF);
115         _env_double_ext =               FFLAG(DOUBLEE_DEF);
116         _env_floating =                 FFLAG(FLOAT_DEF);
117         _env_floating_stack =   fstack_size;
118         _env_max_float =                MAX_F;
119         _env_floating_ext =     FFLAG(FLOATE_DEF);
120         _env_memory_alloc =     FFLAG(MEMALL_DEF);
121         _env_memory_alloc_ext = FFLAG(MEMALLE_DEF);
122         _env_search_order =             FFLAG(SEARCH_DEF);
123         _env_search_order_ext = FFLAG(SEARCHE_DEF);
124         _env_wordlists =                WORD_LISTS;
125         _env_tools =                    FFLAG(TOOLS_DEF);
126         _env_tools_ext =                FFLAG(TOOLSE_DEF);
127         _env_number_locals =    MAX_LOCALS;
128         _env_locals =                   FFLAG(LOCALS_DEF);
129         _env_locals_ext =               FFLAG(LOCALSE_DEF);
130         _env_facility =                 FFLAG(FACILITY_DEF);
131         _env_facility_ext =             FFLAG(FACILITYE_DEF);
132         _env_block =                    FFLAG(BLOCK_DEF);
133         _env_block_ext =                FFLAG(BLOCKE_DEF);
134         _env_exception =                FFLAG(EXCEPTION_DEF);
135         _env_exception_ext =    FFLAG(EXCEPTIONE_DEF);
136         _env_file =                             FFLAG(FILE_DEF);
137         _env_file_ext =                 FFLAG(FILEE_DEF);
138         _env_string =                   FFLAG(STRING_DEF);
139         _env_string_ext =               FFLAG(STRINGE_DEF);
140 }
141