FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
daScript/src/simulate/simulate.cpp at split-heap · Bais/daScript · GitHub
Bais
/
daScript
Public
forked from
GaijinEntertainment/daScript
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
daScript
/
src
/
simulate
/
simulate.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
473 lines (413 loc) · 15.1 KB
Breadcrumbs
daScript
/
src
/
simulate
/
simulate.cpp
Copy path
File metadata and controls
473 lines (413 loc) · 15.1 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
#
include
"
daScript/misc/platform.h
"
#
include
"
daScript/simulate/simulate.h
"
#
include
"
daScript/simulate/runtime_string.h
"
//
this is here for the default implementation of to_out and to_err
#
include
<
iostream
>
namespace
das
{
bool
SimNode::evalBool
( Context & context ) {
assert
(
0
&&
"
we should never be here
"
);
return
cast<
bool
>::
to
(
eval
(context));
}
float
SimNode::evalFloat
( Context & context ) {
assert
(
0
&&
"
we should never be here
"
);
return
cast<
float
>::
to
(
eval
(context));
}
int32_t
SimNode::evalInt
( Context & context ) {
assert
(
0
&&
"
we should never be here
"
);
return
cast<
int32_t
>::
to
(
eval
(context));
}
uint32_t
SimNode::evalUInt
( Context & context ) {
assert
(
0
&&
"
we should never be here
"
);
return
cast<
uint32_t
>::
to
(
eval
(context));
}
int64_t
SimNode::evalInt64
( Context & context ) {
assert
(
0
&&
"
we should never be here
"
);
return
cast<
int64_t
>::
to
(
eval
(context));
}
uint64_t
SimNode::evalUInt64
( Context & context ) {
assert
(
0
&&
"
we should never be here
"
);
return
cast<
uint64_t
>::
to
(
eval
(context));
}
char
*
SimNode::evalPtr
( Context & context ) {
assert
(
0
&&
"
we should never be here
"
);
return
cast<
char
*>::
to
(
eval
(context));
}
vec4f
SimNode_Swizzle::eval
( Context & context ) {
union
{
vec4f res;
float
val[
4
];
} R, S;
S.
res
= value->
eval
(context);
DAS_EXCEPTION_POINT
;
R.
val
[
0
] = S.
val
[fields[
0
]];
R.
val
[
1
] = S.
val
[fields[
1
]];
R.
val
[
2
] = S.
val
[fields[
2
]];
R.
val
[
3
] = S.
val
[fields[
3
]];
return
R.
res
;
}
//
SimNode_MakeBlock
vec4f
SimNode_MakeBlock::eval
( Context & context ) {
Block block;
block.
stackOffset
= context.
stack
.
spi
();
block.
argumentsOffset
= argStackTop ? (context.
stack
.
spi
() + argStackTop) :
0
;
block.
body
= subexpr;
return
cast<Block>::
from
(block);
}
//
SimNode_Call
vec4f
SimNode_Call::eval
( Context & context ) {
vec4f * argValues = (vec4f *)(
alloca
(nArguments *
sizeof
(vec4f)));
evalArgs
(context, argValues);
DAS_EXCEPTION_POINT
;
return
context.
call
(fnIndex, argValues,
nullptr
, debug.
line
);
}
vec4f
SimNode_CallAndCopyOrMove::eval
( Context & context ) {
vec4f * argValues = (vec4f *)(
alloca
(nArguments *
sizeof
(vec4f)));
evalArgs
(context, argValues);
DAS_EXCEPTION_POINT
;
auto
cmres = context.
stack
.
sp
() + stackTop;
return
context.
call
(fnIndex, argValues, cmres, debug.
line
);
}
//
SimNode_Invoke
vec4f
SimNode_Invoke::eval
( Context & context ) {
vec4f * argValues = (vec4f *)(
alloca
(nArguments *
sizeof
(vec4f)));
evalArgs
(context, argValues);
DAS_EXCEPTION_POINT
;
Block block = cast<Block>::
to
(argValues[
0
]);
if
( nArguments>
1
) {
return
context.
invoke
(block, argValues +
1
,
nullptr
);
}
else
{
return
context.
invoke
(block,
nullptr
,
nullptr
);
}
}
vec4f
SimNode_InvokeAndCopyOrMove::eval
( Context & context ) {
vec4f * argValues = (vec4f *)(
alloca
(nArguments *
sizeof
(vec4f)));
evalArgs
(context, argValues);
DAS_EXCEPTION_POINT
;
Block block = cast<Block>::
to
(argValues[
0
]);
auto
cmres = context.
stack
.
sp
() + stackTop;
if
( nArguments>
1
) {
return
context.
invoke
(block, argValues +
1
, cmres);
}
else
{
return
context.
invoke
(block,
nullptr
, cmres);
}
}
//
SimNode_Debug
vec4f
SimNode_Debug::eval
( Context & context ) {
vec4f res = subexpr->
eval
(context);
DAS_EXCEPTION_POINT
;
stringstream ssw;
if
( message ) ssw << message <<
"
"
;
ssw <<
debug_type
(typeInfo) <<
"
=
"
<<
debug_value
(res, typeInfo, PrintFlags::debugger)
<<
"
at
"
<< debug.
describe
() <<
"
\n
"
;
context.
to_out
(ssw.
str
().
c_str
());
return
res;
}
//
SimNode_Assert
vec4f
SimNode_Assert::eval
( Context & context ) {
if
( !subexpr->
evalBool
(context) ) {
DAS_EXCEPTION_POINT
;
string error_message =
"
assert failed
"
;
if
( message )
error_message = error_message +
"
,
"
+ message;
string error =
reportError
(context.
debugInput
, debug.
line
, debug.
column
, error_message );
error = context.
getStackWalk
(
false
) + error;
context.
to_err
(error.
c_str
());
context.
throw_error
(
"
assert failed
"
);
}
return
v_zero
();
}
//
SimNode_TryCatch
vec4f
SimNode_TryCatch::eval
( Context & context ) {
#
if
DAS_ENABLE_EXCEPTIONS
try_block->
eval
(context);
if
( context.
stopFlags
& EvalFlags::stopForThrow ) {
context.
stopFlags
&= ~(EvalFlags::stopForThrow | EvalFlags::stopForReturn | EvalFlags::stopForBreak);
catch_block->
eval
(context);
}
#
else
try
{
try_block->
eval
(context);
}
catch
(
const
runtime_error & ) {
context.
stopFlags
&= ~(EvalFlags::stopForThrow | EvalFlags::stopForReturn | EvalFlags::stopForBreak);
catch_block->
eval
(context);
}
#
endif
return
v_zero
();
}
//
SimNode_New
vec4f
SimNode_New::eval
( Context & context ) {
if
(
void
* ptr = context.
heap
.
allocate
(bytes) ) {
memset
( ptr,
0
, bytes );
return
cast<
void
*>::
from
(ptr);
}
else
{
context.
throw_error
(
"
out of memory
"
);
return
v_zero
();
}
}
//
SimNode_CopyRefValue
vec4f
SimNode_CopyRefValue::eval
( Context & context ) {
auto
pl = l->
evalPtr
(context);
DAS_EXCEPTION_POINT
;
auto
pr = r->
evalPtr
(context);
DAS_EXCEPTION_POINT
;
memcpy
( pl, pr, size );
return
v_zero
();
}
//
SimNode_MoveRefValue
vec4f
SimNode_MoveRefValue::eval
( Context & context ) {
auto
pl = l->
evalPtr
(context);
DAS_EXCEPTION_POINT
;
auto
pr = r->
evalPtr
(context);
DAS_EXCEPTION_POINT
;
memcpy
( pl, pr, size );
memset
( pr,
0
, size );
return
v_zero
();
}
//
SimNode_Block
vec4f
SimNode_Block::eval
( Context & context ) {
for
(
uint32_t
i =
0
; i!=total && !context.
stopFlags
; ++i )
list[i]->
eval
(context);
return
v_zero
();
}
vec4f
SimNode_ClosureBlock::eval
( Context & context ) {
for
(
uint32_t
i =
0
; i!=total && !context.
stopFlags
; ++i )
list[i]->
eval
(context);
if
( context.
stopFlags
& EvalFlags::stopForReturn ) {
context.
stopFlags
&= ~EvalFlags::stopForReturn;
return
context.
abiResult
();
}
else
{
if
( needResult ) context.
throw_error
(
"
end of block without return
"
);
return
v_zero
();
}
}
//
SimNode_Let
vec4f
SimNode_Let::eval
( Context & context ) {
for
(
uint32_t
i =
0
; i!=total && !context.
stopFlags
; ++i )
list[i]->
eval
(context);
DAS_EXCEPTION_POINT
;
return
subexpr ? subexpr->
eval
(context) :
v_zero
();
}
//
SimNode_IfThenElse
vec4f
SimNode_IfThenElse::eval
( Context & context ) {
bool
cmp = cond->
evalBool
(context);
DAS_EXCEPTION_POINT
;
if
( cmp ) {
return
if_true->
eval
(context);
}
else
if
( if_false ) {
return
if_false->
eval
(context);
}
else
{
return
v_zero
();
}
}
//
SimNode_While
vec4f
SimNode_While::eval
( Context & context ) {
while
( cond->
evalBool
(context) && !context.
stopFlags
) {
body->
eval
(context);
}
context.
stopFlags
&= ~EvalFlags::stopForBreak;
return
v_zero
();
}
//
Return
vec4f
SimNode_Return::eval
( Context & context ) {
if
( subexpr ) context.
abiResult
() = subexpr->
eval
(context);
context.
stopFlags
|= EvalFlags::stopForReturn;
return
v_zero
();
}
vec4f
SimNode_ReturnAndCopy::eval
( Context & context ) {
auto
pr = subexpr->
evalPtr
(context);
DAS_EXCEPTION_POINT
;
auto
pl = context.
abiCopyOrMoveResult
();
memcpy
( pl, pr, size);
context.
abiResult
() = cast<
char
*>::
from
(pl);
context.
stopFlags
|= EvalFlags::stopForReturn;
return
v_zero
();
}
vec4f
SimNode_ReturnAndMove::eval
( Context & context ) {
auto
pr = subexpr->
evalPtr
(context);
DAS_EXCEPTION_POINT
;
auto
pl = context.
abiCopyOrMoveResult
();
memcpy
( pl, pr, size);
memset
( pr,
0
, size);
context.
abiResult
() = cast<
char
*>::
from
(pl);
context.
stopFlags
|= EvalFlags::stopForReturn;
return
v_zero
();
}
vec4f
SimNode_ReturnReference::eval
( Context & context ) {
char
* ref = subexpr->
evalPtr
(context);
if
( context.
stack
.
bottom
()<=ref && ref<context.
stack
.
sp
()) {
context.
throw_error
(
"
reference bellow current function stack frame
"
);
return
v_zero
();
}
#
if
DAS_ENABLE_STACK_WALK
auto
pp = (Prologue *) context.
stack
.
sp
();
auto
top = context.
stack
.
sp
() + pp->
info
->
stackSize
;
if
( context.
stack
.
sp
()<=ref && ref<top ) {
context.
throw_error
(
"
reference to current function stack frame
"
);
return
v_zero
();
}
#
endif
context.
abiResult
() = cast<
char
*>::
from
(ref);
context.
stopFlags
|= EvalFlags::stopForReturn;
return
v_zero
();
}
vec4f
SimNode_ReturnAndCopyFromBlock::eval
( Context & context ) {
auto
pr = subexpr->
evalPtr
(context);
DAS_EXCEPTION_POINT
;
auto
ba = (BlockArguments *) ( context.
stack
.
sp
() + argStackTop );
auto
pl = ba->
copyOrMoveResult
;
memcpy
( pl, pr, size);
context.
abiResult
() = cast<
char
*>::
from
(pl);
context.
stopFlags
|= EvalFlags::stopForReturn;
return
v_zero
();
}
vec4f
SimNode_ReturnAndMoveFromBlock::eval
( Context & context ) {
auto
pr = subexpr->
evalPtr
(context);
DAS_EXCEPTION_POINT
;
auto
ba = (BlockArguments *) ( context.
stack
.
sp
() + argStackTop );
auto
pl = ba->
copyOrMoveResult
;
memcpy
( pl, pr, size);
memset
( pr,
0
, size);
context.
abiResult
() = cast<
char
*>::
from
(pl);
context.
stopFlags
|= EvalFlags::stopForReturn;
return
v_zero
();
}
vec4f
SimNode_ReturnReferenceFromBlock::eval
( Context & context ) {
char
* ref = subexpr->
evalPtr
(context);
if
( context.
stack
.
bottom
()<=ref && ref<context.
stack
.
ap
() ) {
context.
throw_error
(
"
reference bellow current call chain stack frame
"
);
return
v_zero
();
}
context.
abiResult
() = cast<
char
*>::
from
(ref);
context.
stopFlags
|= EvalFlags::stopForReturn;
return
v_zero
();
}
//
Context
Context::Context
(
const
string * lines,
uint32_t
heapSize)
: heap(heapSize)
, code(
64
*
1024
)
, debugInfo(
64
*
1024
)
, stack(
16
*
1024
)
{
debugInput = lines;
}
#
ifdef
_MSC_VER
#
pragma
warning(push)
#
pragma
warning(disable:4324)
#
pragma
warning(disable:4701)
#
endif
vec4f
Context::invokeEx
(
const
Block &block, vec4f * args,
void
* cmres, function<
void
(SimNode *)> && when) {
auto
watermark = stack.
invoke
(block.
stackOffset
);
BlockArguments * ba =
nullptr
;
BlockArguments saveArguments;
if
( block.
argumentsOffset
) {
ba = (BlockArguments *) ( stack.
bottom
() + block.
argumentsOffset
);
saveArguments = *ba;
ba->
arguments
= args;
ba->
copyOrMoveResult
= (
char
*) cmres;
}
when
(block.
body
);
if
( ba ) {
*ba = saveArguments;
}
stack.
pop
(watermark);
return
result;
}
#
ifdef
_MSC_VER
#
pragma
warning(pop)
#
endif
vec4f
Context::callEx
(
int
fnIndex, vec4f *args,
void
* cmres,
int
line, function<
void
(SimNode *)> && when) {
assert
(fnIndex>=
0
&& fnIndex<totalFunctions &&
"
function index out of range
"
);
auto
& fn = functions[fnIndex];
//
PUSH
auto
watermark = stack.
push
(fn.
stackSize
);
if
( watermark==-
1u
) {
throw_error
(
"
stack overflow
"
);
return
v_zero
();
}
//
fill prologue
Prologue * pp = (Prologue *) stack.
sp
();
pp->
arguments
= args;
pp->
copyOrMoveResult
= (
char
*)cmres;
#
if
DAS_ENABLE_STACK_WALK
pp->
info
= fn.
debug
;
pp->
line
= line;
#
endif
//
CALL
when
(fn.
code
);
stopFlags &= ~(EvalFlags::stopForReturn | EvalFlags::stopForBreak);
//
POP
stack.
pop
(watermark);
return
result;
}
void
Context::runInitScript
(
void
) {
for
(
int
i=
0
; i!=totalVariables && !stopFlags; ++i ) {
auto
& pv = globalVariables[i];
if
( pv.
init
) {
pv.
init
->
eval
(*
this
);
}
else
{
memset
( cast<
char
*>::
to
(pv.
value
),
0
, pv.
size
);
}
}
}
int
Context::findFunction
(
const
char
* name )
const
{
for
(
int
fni =
0
; fni != totalFunctions; ++fni ) {
if
(
strcmp
(functions[fni].
name
, name)==
0
) {
return
fni;
}
}
return
-
1
;
}
int
Context::findVariable
(
const
char
* name )
const
{
for
(
int
vni =
0
; vni != totalVariables; ++vni ) {
if
(
strcmp
(globalVariables[vni].
name
, name)==
0
) {
return
vni;
}
}
return
-
1
;
}
void
Context::stackWalk
() {
auto
str =
getStackWalk
();
to_out
(str.
c_str
());
}
string
Context::getStackWalk
(
bool
args ) {
stringstream ssw;
#
if
DAS_ENABLE_STACK_WALK
ssw <<
"
\n
CALL STACK (sp=
"
<< (stack.
top
() - stack.
sp
()) <<
"
):
\n
"
;
char
* sp = stack.
sp
();
while
( sp < stack.
top
() ) {
Prologue * pp = (Prologue *) sp;
if
( pp->
line
) {
ssw << pp->
info
->
name
<<
"
at line
"
<< pp->
line
<<
"
(sp=
"
<< (stack.
top
() - sp) <<
"
)
\n
"
;
}
else
{
ssw << pp->
info
->
name
<<
"
(sp=
"
<< (stack.
top
() - sp) <<
"
)
\n
"
;
}
if
( args ) {
for
(
uint32_t
i =
0
; i != pp->
info
->
argsSize
; ++i ) {
ssw <<
"
\t
"
<< pp->
info
->
args
[i]->
name
<<
"
:
"
<<
debug_type
(pp->
info
->
args
[i])
<<
"
=
\t
"
<<
debug_value
(pp->
arguments
[i], pp->
info
->
args
[i], PrintFlags::stackwalker) <<
"
\n
"
;
}
}
sp += pp->
info
->
stackSize
;
}
ssw <<
"
\n
"
;
#
else
ssw <<
"
\n
CALL STACK TRACKING DISABLED:
\n\n
"
;
#
endif
return
ssw.
str
();
}
void
Context::breakPoint
(
int
,
int
)
const
{
#
ifdef
_MSC_VER
__debugbreak
();
#
else
raise
(
SIGTRAP
);
#
endif
}
void
Context::to_out
(
const
char
* message ) {
cout << message;
}
void
Context::to_err
(
const
char
* message ) {
cerr << message;
}
}
Back
|
FazBrowse Home
|
New Git URL