summaryrefslogtreecommitdiff
path: root/src/runtime/c/teyjus/simulator/siminit.c
blob: b6de2aceacfc564efcc3a1575d482f317d0546d9 (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
//////////////////////////////////////////////////////////////////////////////
//Copyright 2008
//  Andrew Gacek, Steven Holte, Gopalan Nadathur, Xiaochu Qi, Zach Snow
//////////////////////////////////////////////////////////////////////////////
// This file is part of Teyjus.                                             //
//                                                                          //
// Teyjus is free software: you can redistribute it and/or modify           //
// it under the terms of the GNU General Public License as published by     //
// the Free Software Foundation, either version 3 of the License, or        //
// (at your option) any later version.                                      //
//                                                                          //
// Teyjus is distributed in the hope that it will be useful,                //
// but WITHOUT ANY WARRANTY; without even the implied warranty of           //
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            //
// GNU General Public License for more details.                             //
//                                                                          //
// You should have received a copy of the GNU General Public License        //
// along with Teyjus.  If not, see <http://www.gnu.org/licenses/>.          //
//////////////////////////////////////////////////////////////////////////////
/**************************************************************************/
/*                                                                        */
/*  File siminit.c.                                                       */
/**************************************************************************/
#ifndef SIMINIT_C
#define SIMINIT_C

#include "siminit.h"
#include "abstmachine.h"
#include "dataformats.h"
#include "io-datastructures.h"
#include "builtins/builtins.h"
#include "../tables/instructions.h"
#include "../system/error.h"
#include "../system/message.h"

#include <stdio.h>
/***************************######********************************************
 *                          ERROR INFORMATION
 *********************************######**************************************/
static MSG_Msg SIM_errorMessages[SIM_NUM_ERROR_MESSAGES] =
{
   { SIM_ERROR,
     0,
     "Simulator: ",
     0, 0, 0 },
   { SIM_ERROR_TOO_MANY_ABSTRACTIONS,
     SIM_ERROR,
     "Abstraction embedding depth has exceeded maximum of %d.",
     EM_NEWLINE, EM_TOP_LEVEL, 4 },
   { SIM_ERROR_TOO_MANY_ARGUMENTS,
     SIM_ERROR,
     "Application arguments has exceeded maximum of %d.",
     EM_NEWLINE, EM_TOP_LEVEL, 4 },
   { SIM_ERROR_TOO_MANY_UNIV_QUANTS,
     SIM_ERROR,
     "Too many universal quantifiers.",
     EM_NEWLINE, EM_TOP_LEVEL, 3 },
   { SIM_ERROR_HEAP_TOO_BIG,
     SIM_ERROR,
     "Specified heap size (%uK) is larger than maximum of 256Gb.",
     EM_NEWLINE, EM_ABORT, 1 },
   { SIM_ERROR_HEAP_TOO_SMALL,
     SIM_ERROR,
     "Specified heap size (%uK) is smaller than minimum of 10K.",
     EM_NEWLINE, EM_ABORT, 1 },
   { SIM_ERROR_CANNOT_ALLOCATE_HEAP,
     SIM_ERROR_CANNOT_ALLOCATE_HEAP_MESSAGE,
     "",
     SIM_ERROR_CANNOT_ALLOCATE_HEAP_SUGGESTION, EM_ABORT, 1 },
   { SIM_ERROR_CANNOT_ALLOCATE_HEAP_MESSAGE,
     SIM_ERROR,
     "Could not allocate heap of size %uK at 0x%08x using %s.",
     EM_NEWLINE, EM_NO_EXN, 1 },
   { SIM_ERROR_CANNOT_ALLOCATE_HEAP_SUGGESTION,
     SIM_ERROR,
     "Try modifying the configuration and recompiling.",
     EM_NEWLINE, EM_NO_EXN, 1 },
   { SIM_ERROR_TRAIL_OVERFL,
     SIM_ERROR,
     "Trail overflow.",
     EM_NEWLINE, EM_TOP_LEVEL, 1 },
   { SIM_ERROR_HEAP_OVERFL,
     SIM_ERROR,
     "Heap overflow.",
     EM_NEWLINE, EM_TOP_LEVEL, 1 },
   { SIM_ERROR_STACK_OVERFL,
     SIM_ERROR,
     "Stack overflow.",
     EM_NEWLINE, EM_TOP_LEVEL, 1 },
   { SIM_ERROR_PDL_OVERFL,
     SIM_ERROR,
     "PDL overflow.",
     EM_NEWLINE, EM_TOP_LEVEL, 1 }
};


/*************************************************************************/
/*            SETTING UP SPECIAL CODE SEGMENTS                           */
/*************************************************************************/
static const int SINIT_initSize = 31;

static void SINIT_initCode()
{
    MemPtr    nhreg  = AM_hreg + SINIT_initSize;
    CSpacePtr myhreg = (CSpacePtr)AM_hreg;
    
    AM_heapError(nhreg);
    
    //builtinCode
    AM_builtinCode = myhreg;
    *((INSTR_OpCode*)myhreg) = builtin;         //builtin ...
    myhreg += INSTR_I1X_LEN;
    
    //eqCode
    AM_eqCode = myhreg;
    *((INSTR_OpCode*)myhreg) = pattern_unify_t; //pattern_unify A1, A2
    *((INSTR_RegInd*)(myhreg + INSTR_RRX_R1)) = 1;
    *((INSTR_RegInd*)(myhreg + INSTR_RRX_R2)) = 2;
    myhreg += INSTR_RRX_LEN;
    *((INSTR_OpCode*)myhreg) = finish_unify;    //finish_unify
    myhreg += INSTR_X_LEN;
    *((INSTR_OpCode*)myhreg) = proceed;         //proceed
    myhreg += INSTR_X_LEN;
    
    //failCode
    AM_failCode = myhreg;
    *((INSTR_OpCode*)myhreg) = fail;            //fail
    myhreg += INSTR_X_LEN;
    
    //andCode
    *((INSTR_OneByteInt*)(myhreg + INSTR_I1LX_I1)) = 2;//"call" 2 L
    myhreg += INSTR_I1LX_LEN;
    AM_andCode = myhreg;
    *((INSTR_OpCode*)myhreg) = put_value_p;            //put_value Y1, A1
    *((INSTR_EnvInd*)(myhreg + INSTR_ERX_E)) = 1;
    *((INSTR_RegInd*)(myhreg + INSTR_ERX_R)) = 1;
    myhreg += INSTR_ERX_LEN;
    *((INSTR_OpCode*)myhreg) = put_level;              //put_level Y2
    *((INSTR_EnvInd*)(myhreg + INSTR_EX_E))  = 2;
    myhreg += INSTR_EX_LEN;
    *((INSTR_OpCode*)myhreg) = deallocate;             //deallocate
    myhreg += INSTR_X_LEN;
    
    //solveCode
    AM_solveCode = myhreg;
    *((INSTR_OpCode*)myhreg) = builtin;         //builtin BI_SOLVE
    *((INSTR_OneByteInt*)(myhreg + INSTR_I1X_I1)) = BI_SOLVE;
    myhreg += INSTR_I1X_LEN;
    
    //proceed
    AM_proceedCode = myhreg;                    //proceed
    *((INSTR_OpCode*)myhreg) = proceed;
    myhreg += INSTR_X_LEN;
    
    //orCode
    AM_orCode = myhreg;
    *((INSTR_OpCode*)myhreg) = trust_me;        //trust_me 1 
    *((INSTR_OneByteInt*)(myhreg + INSTR_I1WPX_I1)) = 1;
    myhreg += INSTR_I1WPX_LEN;
    *((INSTR_OpCode*)myhreg) = builtin;         //builtin BI_SOLVE
    *((INSTR_OneByteInt*)(myhreg + INSTR_I1X_I1)) = BI_SOLVE;
    myhreg += INSTR_I1X_LEN;
    
    //allcode
    *((INSTR_OneByteInt*)(myhreg + INSTR_I1LX_I1)) = 0; //"call" 0 L
    myhreg += INSTR_I1LX_LEN;
    AM_allCode = myhreg;
    *((INSTR_OpCode*)myhreg) = decr_universe;           //decr_universe
    myhreg += INSTR_X_LEN;
    *((INSTR_OpCode*)myhreg) = deallocate;              //deallocate
    myhreg += INSTR_X_LEN;
    *((INSTR_OpCode*)myhreg) = proceed;                 //proceed
    myhreg += INSTR_X_LEN;
    
    //stopCode
    AM_stopCode = myhreg;
    *((INSTR_OpCode*)myhreg) = stop;            //stop
    myhreg += INSTR_X_LEN;
    
    //notCode2   
    AM_notCode2 = myhreg;
    *((INSTR_OpCode*)myhreg) = trust_me;        //trust_me 0
    *((INSTR_OneByteInt*)(myhreg + INSTR_I1WPX_I1)) = 0;
    myhreg += INSTR_I1WPX_LEN;
    *((INSTR_OpCode*)myhreg) = proceed;         //proceed
    myhreg += INSTR_X_LEN;
    
    //notCode1
    AM_notCode1 = myhreg;
    *((INSTR_OpCode*)myhreg) = allocate;        //allocate 2
    *((INSTR_OneByteInt*)(myhreg + INSTR_I1X_I1)) = 2;
    myhreg += INSTR_I1X_LEN;
    *((INSTR_OpCode*)myhreg) = get_level;       //get_level Y1
    *((INSTR_EnvInd*)(myhreg + INSTR_EX_E)) = 1;
    myhreg += INSTR_EX_LEN;
    *((INSTR_OpCode*)myhreg) = call_builtin;    //call_builtin 1 BI_SOLVE
    *((INSTR_OneByteInt*)(myhreg + INSTR_I1I1WPX_I11)) = 1;
    *((INSTR_OneByteInt*)(myhreg + INSTR_I1I1WPX_I12)) = BI_SOLVE;
    myhreg += INSTR_I1I1WPX_LEN;
    *((INSTR_OpCode*)myhreg) = cut;             //cut 1
    *((INSTR_EnvInd*)(myhreg + INSTR_EX_E)) = 1;
    myhreg += INSTR_EX_LEN;
    *((INSTR_OpCode*)myhreg) = fail;            //fail
    myhreg += INSTR_X_LEN;
    
    //haltCode
    AM_haltCode = myhreg;
    *((INSTR_OpCode*)myhreg) = halt;            //halt
    myhreg += INSTR_X_LEN;
    
    AM_hreg = nhreg;
}

/*****************************************************************************
 *                          THE PUBLIC ROUTINES                              *
 *****************************************************************************/
void SINIT_preInit()
{
    /* errors get initialized before ANYTHING */
    MSG_addMessages(SIM_NUM_ERROR_MESSAGES, SIM_errorMessages);
}

void SINIT_simInit()
{
    AM_hreg  = AM_heapBeg;           //heap
    AM_hbreg = AM_heapBeg;
    AM_ereg  = AM_stackBeg;          //stack
    AM_ireg  = AM_stackBeg;
    AM_cireg = AM_stackBeg;
    AM_initPDL();                    //pdl 
    AM_trreg = AM_trailBeg;          //trail
    AM_llreg = DF_EMPTY_DIS_SET;     //live list
    AM_bndFlag = OFF;                //bind flag
    AM_ucreg = 0;                    //uc reg

    //make a dummy first mod point at the beginning of the stack
    AM_mkDummyImptRec(AM_ireg);

    /* perform initialization for the term io system */
    IO_initIO();

    /* and set up some built-in code */
    SINIT_initCode();

    /* set up the base branch register to put the heap back to this point */
    AM_breg = AM_stackBeg + AM_DUMMY_IMPT_REC_SIZE;
    *AM_breg = (Mem)AM_hreg;
    
    AM_fstCP = AM_b0reg = AM_breg;
    AM_tosreg = AM_breg + 1;
}

void SINIT_reInitSimState(Boolean inDoInitializeImports)
{   
    AM_initPDL();                 //pdl
    AM_ereg  = AM_stackBeg;       //stack
    AM_trreg = AM_trailBeg;       //trail
    AM_llreg = DF_EMPTY_DIS_SET;  //live list
    AM_ucreg = 0;                 //uc reg
    AM_bndFlag = OFF;             //bind flag
    AM_breg = AM_fstCP;
    AM_hreg = AM_cpH();
    AM_hreg = *((MemPtr *)AM_breg);
    
    /* initialize ireg if necessary */
    if (inDoInitializeImports) {
        AM_ireg = AM_stackBeg;
        AM_tosreg = AM_breg + 1;
    }

    IO_initIO();
}


#endif //SIMINIT_H