FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
daScript/src/simulate/simulate_fn_hash.cpp at master · nolankramer/daScript · GitHub
nolankramer
/
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_fn_hash.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
280 lines (266 loc) · 10.4 KB
Breadcrumbs
daScript
/
src
/
simulate
/
simulate_fn_hash.cpp
Copy path
File metadata and controls
280 lines (266 loc) · 10.4 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
#
include
"
daScript/misc/platform.h
"
#
include
"
daScript/ast/ast.h
"
#
include
"
daScript/simulate/hash.h
"
#
include
<
cinttypes
>
namespace
das
{
#
if
1
#
define
debug_hash
(...)
#
else
#define debug_hash printf
#
endif
#
if
1
#
define
debug_aot_hash
(...)
#
else
#define debug_aot_hash printf
#
endif
struct
SimFnHashVisitor
: SimVisitor {
Context * context =
nullptr
;
SimFnHashVisitor
( Context * ctx )
: context(ctx) {
}
//
64 bit FNV1a
const
uint64_t
fnv_prime =
1099511628211ul
;
uint64_t
offset_basis =
14695981039346656037ul
;
__forceinline
void
write
(
const
void
* pb,
uint32_t
size ) {
const
uint8_t
* block = (
const
uint8_t
*) pb;
while
( size-- ) {
offset_basis = ( offset_basis ^ *block++ ) * fnv_prime;
}
debug_hash
(
"
%
"
PRIx64
"
"
, offset_basis);
}
__forceinline
void
write
(
const
void
* pb ) {
const
uint8_t
* block = (
const
uint8_t
*) pb;
for
(; *block; block++) {
offset_basis = ( offset_basis ^ *block ) * fnv_prime;
}
debug_hash
(
"
[%s] %
"
PRIx64
"
"
, (
const
char
*) pb, offset_basis);
}
__forceinline
uint64_t
getHash
()
const
{
return
(offset_basis <=
1
) ? fnv_prime : offset_basis;
}
//
semantic walker
virtual
void
op
(
const
char
* name,
uint32_t
sz,
const
string &
TT
)
override
{
write
(name);
if
( sz )
write
(&sz,
sizeof
(sz));
if
( !
TT
.
empty
() )
write
(
TT
.
c_str
());
}
virtual
void
sp
(
uint32_t
stackTop,
const
char
* op )
override
{
write
(&stackTop,
sizeof
(stackTop));
write
(op);
}
virtual
void
arg
( Func,
const
char
* mangledName,
const
char
* argN )
override
{
write
(mangledName);
write
(argN);
}
virtual
void
arg
( Func,
uint32_t
mangledNameHash,
const
char
* argN )
override
{
write
(&mangledNameHash,
sizeof
(mangledNameHash));
write
(argN);
}
virtual
void
arg
(
int32_t
argV,
const
char
* argN )
override
{
write
(&argV,
sizeof
(argV));
write
(argN);
}
virtual
void
arg
(
uint32_t
argV,
const
char
* argN )
override
{
write
(&argV,
sizeof
(argV));
write
(argN);
}
virtual
void
arg
(
const
char
* argV,
const
char
* argN )
override
{
if
( argV )
write
(argV);
write
(argN);
}
virtual
void
arg
( vec4f argV,
const
char
* argN )
override
{
write
(&argV,
sizeof
(argV));
write
(argN);
}
virtual
void
arg
(
uint64_t
argV,
const
char
* argN )
override
{
write
(&argV,
sizeof
(argV));
write
(argN);
}
virtual
void
arg
(
bool
argV,
const
char
* argN )
override
{
write
(&argV,
sizeof
(argV));
write
(argN);
}
virtual
void
arg
(
float
argV,
const
char
* argN )
override
{
write
(&argV,
sizeof
(argV));
write
(argN);
}
virtual
void
arg
(
double
argV,
const
char
* argN )
override
{
write
(&argV,
sizeof
(argV));
write
(argN);
}
virtual
void
sub
( SimNode ** nodes,
uint32_t
count,
const
char
* argN )
override
{
write
(&count,
sizeof
(count));
write
(argN);
for
(
uint32_t
t=
0
; t!=count; ++t ) {
nodes[t] = nodes[t]->
visit
(*
this
);
}
}
virtual
SimNode *
sub
( SimNode * node,
const
char
* opN )
override
{
write
(opN);
return
SimVisitor::sub
(node, opN);
}
};
uint64_t
getSemanticHash
( SimNode * node, Context * context ) {
debug_hash
(
"
\n
"
);
SimFnHashVisitor
hashV
(context);
node->
visit
(hashV);
debug_hash
(
"
\n
"
);
return
hashV.
getHash
();
}
uint64_t
getFunctionHash
( Function * fun, SimNode * node, Context * context ) {
debug_hash
(
"
\n
%s
\n
"
, fun->
getMangledName
().
c_str
());
SimFnHashVisitor
hashV
(context);
//
append return type and result type
uint64_t
resT = fun->
result
->
getSemanticHash
();
hashV.
write
(&resT,
sizeof
(
uint64_t
));
debug_hash
(
"
\n
result <%s> = %
"
PRIx64
"
\n
"
, fun->
result
->
getMangledName
().
c_str
(), resT);
for
(
auto
& arg : fun->
arguments
) {
uint64_t
argT = arg->
type
->
getSemanticHash
();
hashV.
write
(&argT,
sizeof
(argT));
debug_hash
(
"
arg %s <%s> = %
"
PRIx64
"
\n
"
, arg->
type
->
getMangledName
().
c_str
(), arg->
name
.
c_str
(), argT);
}
//
append code
node->
visit
(hashV);
if
( fun->
aotHashDeppendsOnArguments
) {
for
(
auto
& arg : fun->
arguments
) {
hashV.
write
(arg->
name
.
c_str
());
if
( arg->
init
) {
TextWriter tw;
tw << *(arg->
init
);
hashV.
write
(tw.
str
().
c_str
());
}
}
}
uint64_t
res = hashV.
getHash
();
debug_hash
(
"
\n
%s = %
"
PRIx64
"
\n
"
, fun->
getMangledName
().
c_str
(), res);
return
res;
}
struct
DependencyCollector
{
void
collect
(
const
Function * fun ) {
if
( functions.
find
(fun) != functions.
end
() )
return
;
functions.
insert
(fun);
for
(
const
auto
& depF : fun->
useFunctions
) {
collect
(depF);
}
for
(
const
auto
& depV : fun->
useGlobalVariables
) {
collect
(depV);
}
}
void
collect
(
const
Variable * var ) {
if
( variables.
find
(var) != variables.
end
() )
return
;
variables.
insert
(var);
for
(
const
auto
& depF : var->
useFunctions
) {
collect
(depF);
}
for
(
const
auto
& depV : var->
useGlobalVariables
) {
collect
(depV);
}
}
using
FunAndName = pair<
const
Function *,string>;
vector<FunAndName>
getStableDependencies
() {
vector<FunAndName> vec;
vec.
reserve
(functions.
size
());
for
(
const
auto
& it : functions ) {
vec.
emplace_back
(
make_pair
(it,it->
getMangledName
()));
}
stable_sort
(vec.
begin
(), vec.
end
(), [&](
const
FunAndName & a,
const
FunAndName & b){
return
a.
second
< b.
second
;
});
return
vec;
}
using
VarAndName = pair<
const
Variable *,string>;
vector<VarAndName>
getStableVariableDependencies
() {
vector<VarAndName> vec;
vec.
reserve
(variables.
size
());
for
(
const
auto
& it : variables ) {
vec.
emplace_back
(
make_pair
(it,it->
getMangledName
()));
}
stable_sort
(vec.
begin
(), vec.
end
(), [&](
const
VarAndName & a,
const
VarAndName & b){
return
a.
second
< b.
second
;
});
return
vec;
}
das_hash_set<
const
Function *> functions;
das_hash_set<
const
Variable *> variables;
};
void
collectDependencies
( FunctionPtr fun,
const
TBlock<
void
,TArray<Function *>,TArray<Variable *>> & block, Context * context, LineInfoArg * line ) {
auto
program =
daScriptEnvironment::getBound
()->
g_Program
;
if
( !program ) context->
throw_error_at
(line,
"
Can't collect dependencies outside of compilation.
"
);
program->
markExecutableSymbolUse
();
DependencyCollector collector;
collector.
collect
(fun.
get
());
auto
vecFunc = collector.
getStableDependencies
();
auto
vecVars = collector.
getStableVariableDependencies
();
vector<
const
Function *> tfun;
tfun.
reserve
(vecFunc.
size
());
for
(
auto
& fn : vecFunc ) {
tfun.
push_back
(fn.
first
);
}
vector<
const
Variable *> tvar;
tvar.
reserve
(vecVars.
size
());
for
(
auto
& fn : vecVars ) {
tvar.
push_back
(fn.
first
);
}
Array afun, avar;
if
( tfun.
size
() ) {
array_mark_locked
(afun, tfun.
data
(),
uint32_t
(tfun.
size
()));
}
if
( tvar.
size
() ) {
array_mark_locked
(avar, tvar.
data
(),
uint32_t
(tvar.
size
()));
}
vec4f args[
2
];
args[
0
] = cast<Array &>::
from
(afun);
args[
1
] = cast<Array &>::
from
(avar);
context->
invoke
(block, args,
nullptr
, line);
}
uint64_t
getFunctionAotHash
(
const
Function * fun ) {
DependencyCollector collector;
collector.
collect
(fun);
auto
vec = collector.
getStableDependencies
();
vector<
uint64_t
> uvec;
uvec.
reserve
(vec.
size
() +
1
);
debug_aot_hash
(
"
HASH %s %
"
PRIx64
"
\n
"
, fun->
getMangledName
().
c_str
(), fun->
hash
);
uvec.
push_back
(fun->
hash
);
for
(
const
auto
& fn : vec ) {
if
( !fn.
first
->
noAot
&&!fn.
first
->
builtIn
) {
DAS_ASSERTF
(fn.
first
->
hash
,
"
%s has dependency on %s, which hash hash of 0
"
,
fun->
getMangledName
().
c_str
(), fn.
first
->
getMangledName
().
c_str
());
uvec.
push_back
(fn.
first
->
hash
);
debug_aot_hash
(
"
\t
%s %
"
PRIx64
"
\n
"
, fn.
second
.
c_str
(), fn.
first
->
hash
);
}
}
uint64_t
res =
hash_block64
((
const
uint8_t
*)uvec.
data
(),
uint32_t
(uvec.
size
()*
sizeof
(
uint64_t
)));
debug_aot_hash
(
"
AOT HASH %
"
PRIx64
"
\n
"
, res);
return
res;
}
string
getAotHashComment
(
const
Function * fun ) {
DependencyCollector collector;
collector.
collect
(fun);
auto
vec = collector.
getStableDependencies
();
TextWriter tw;
tw << fun->
getMangledName
() <<
"
hash=0x
"
<<
HEX
<< fun->
hash
;
for
(
const
auto
& fn : vec ) {
if
( !fn.
first
->
noAot
&& !fn.
first
->
builtIn
&& fn.
first
!= fun ) {
tw <<
"
,
"
<< fn.
second
<<
"
=0x
"
<< fn.
first
->
hash
;
}
}
tw <<
DEC
;
return
tw.
str
();
}
uint64_t
getVariableListAotHash
(
const
vector<
const
Variable *> & globs,
uint64_t
initHash ) {
DependencyCollector collector;
for
(
const
auto
& var : globs ) {
collector.
collect
(var);
}
auto
vec = collector.
getStableDependencies
();
vector<
uint64_t
> uvec;
uvec.
reserve
(vec.
size
() +
1
);
uvec.
push_back
(initHash);
for
(
const
auto
& fn : vec ) {
if
( !fn.
first
->
noAot
) {
uvec.
push_back
(fn.
first
->
hash
);
}
}
return
hash_block64
((
const
uint8_t
*)uvec.
data
(),
uint32_t
(uvec.
size
()*
sizeof
(
uint64_t
)));
}
}
Back
|
FazBrowse Home
|
New Git URL