-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVM.inl
148 lines (139 loc) · 16.5 KB
/
VM.inl
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
#define APPREGATE(arg, actualArg, line) \
if (IS_NUM_VALUE(arg)) \
{ \
double actualArg = TO_NUM_VALUE(arg); \
line; \
} \
else if (IS_BOOL_VALUE(arg)) \
{ \
bool actualArg = TO_BOOL_VALUE(arg); \
line; \
} \
else if (IS_OBJECT_VALUE(arg)) \
{ \
Object *actualArg = TO_OBJECT_VALUE(arg); \
line; \
}
#define APPREGATE2(arg0, arg1, actualArg0, actualArg1, line) \
if (IS_NUM_VALUE(arg0)) \
{ \
double actualArg0 = TO_NUM_VALUE(arg0); \
APPREGATE(arg1, actualArg1, line) \
} \
else if (IS_BOOL_VALUE(arg0)) \
{ \
bool actualArg0 = TO_BOOL_VALUE(arg0); \
APPREGATE(arg1, actualArg1, line) \
} \
else if (IS_OBJECT_VALUE(arg0)) \
{ \
Object *actualArg0 = TO_OBJECT_VALUE(arg0); \
APPREGATE(arg1, actualArg1, line) \
}
#define APPREGATE3(arg0, arg1, arg2, actualArg0, actualArg1, actualArg2, line) \
if (IS_NUM_VALUE(arg0)) \
{ \
double actualArg0 = TO_NUM_VALUE(arg0); \
APPREGATE2(arg1, arg2, actualArg1, actualArg2, line) \
} \
else if (IS_BOOL_VALUE(arg0)) \
{ \
bool actualArg0 = TO_BOOL_VALUE(arg0); \
APPREGATE2(arg1, arg2, actualArg1, actualArg2, line) \
} \
else if (IS_OBJECT_VALUE(arg0)) \
{ \
Object *actualArg0 = TO_OBJECT_VALUE(arg0); \
APPREGATE2(arg1, arg2, actualArg1, actualArg2, line) \
}
#define APPREGATE4(arg0, arg1, arg2, arg3, actualArg0, actualArg1, actualArg2, actualArg3, line) \
if (IS_NUM_VALUE(arg0)) \
{ \
double actualArg0 = TO_NUM_VALUE(arg0); \
APPREGATE3(arg1, arg2, arg3, actualArg1, actualArg2, actualArg3, line) \
} \
else if (IS_BOOL_VALUE(arg0)) \
{ \
bool actualArg0 = TO_BOOL_VALUE(arg0); \
APPREGATE3(arg1, arg2, arg3, actualArg1, actualArg2, actualArg3, line) \
} \
else if (IS_OBJECT_VALUE(arg0)) \
{ \
Object *actualArg0 = TO_OBJECT_VALUE(arg0); \
APPREGATE3(arg1, arg2, arg3, actualArg1, actualArg2, actualArg3, line) \
}
#define APPREGATE5(arg0, arg1, arg2, arg3, arg4, actualArg0, actualArg1, actualArg2, actualArg3, actualArg4, line) \
if (IS_NUM_VALUE(arg0)) \
{ \
double actualArg0 = TO_NUM_VALUE(arg0); \
APPREGATE4(arg1, arg2, arg3, arg4, actualArg1, actualArg2, actualArg3, actualArg4, line) \
} \
else if (IS_BOOL_VALUE(arg0)) \
{ \
bool actualArg0 = TO_BOOL_VALUE(arg0); \
APPREGATE4(arg1, arg2, arg3, arg4, actualArg1, actualArg2, actualArg3, actualArg4, line) \
} \
else if (TO_OBJECT_VALUE(arg0)) \
{ \
Object *actualArg0 = TO_OBJECT_VALUE(arg0); \
APPREGATE4(arg1, arg2, arg3, arg4, actualArg1, actualArg2, actualArg3, actualArg4, line) \
}
#define APPREGATE6(arg0, arg1, arg2, arg3, arg4, arg5, actualArg0, actualArg1, actualArg2, actualArg3, actualArg4, actualArg5, line) \
if (IS_NUM_VALUE(arg0)) \
{ \
double actualArg0 = TO_NUM_VALUE(arg0); \
APPREGATE5(arg1, arg2, arg3, arg4, arg5, actualArg1, actualArg2, actualArg3, actualArg4, actualArg5, line) \
} \
else if (IS_BOOL_VALUE(arg0)) \
{ \
bool actualArg0 = TO_BOOL_VALUE(arg0); \
APPREGATE5(arg1, arg2, arg3, arg4, arg5, actualArg1, actualArg2, actualArg3, actualArg4, actualArg5, line) \
} \
else if (IS_STR_VALUE(arg0)) \
{ \
Object *actualArg0 = TO_OBJECT_VALUE(arg0); \
APPREGATE5(arg1, arg2, arg3, arg4, arg5, actualArg1, actualArg2, actualArg3, actualArg4, actualArg5, line) \
}
#define RUN(type, ret, initializer) \
type ret initializer; \
if (frame.fn->parameterCount == 1) \
{ \
APPREGATE(*(frame.slot + 0), arg0, ret m_Jit->Run<type>(fnName, std::move(arg0))) \
} \
else if (frame.fn->parameterCount == 2) \
{ \
APPREGATE2(*(frame.slot + 0), *(frame.slot + 1), arg0, arg1, ret m_Jit->Run<type>(fnName, std::move(arg0), std::move(arg1))) \
} \
else if (frame.fn->parameterCount == 3) \
{ \
APPREGATE3(*(frame.slot + 0), *(frame.slot + 1), *(frame.slot + 2), arg0, arg1, arg2, ret m_Jit->Run<type>(fnName, std::move(arg0), std::move(arg1), std::move(arg2))) \
} \
else if (frame.fn->parameterCount == 4) \
{ \
APPREGATE4(*(frame.slot + 0), *(frame.slot + 1), *(frame.slot + 2), *(frame.slot + 3), arg0, arg1, arg2, arg3, ret m_Jit->Run<type>(fnName, std::move(arg0), std::move(arg1), std::move(arg2), std::move(arg3))) \
} \
else if (frame.fn->parameterCount == 5) \
{ \
APPREGATE5(*(frame.slot + 0), *(frame.slot + 1), *(frame.slot + 2), *(frame.slot + 3), *(frame.slot + 4), arg0, arg1, arg2, arg3, arg4, ret m_Jit->Run<type>(fnName, std::move(arg0), std::move(arg1), std::move(arg2), std::move(arg3), std::move(arg4))) \
} \
else if (frame.fn->parameterCount == 6) \
{ \
APPREGATE6(*(frame.slot + 0), *(frame.slot + 1), *(frame.slot + 2), *(frame.slot + 3), *(frame.slot + 4), *(frame.slot + 5), arg0, arg1, arg2, arg3, arg4, arg5, ret m_Jit->Run<type>(fnName, std::move(arg0), std::move(arg1), std::move(arg2), std::move(arg3), std::move(arg4), std::move(arg4))) \
} \
else \
ret m_Jit->Run<type>(fnName);
template <typename T>
inline void VM::ExecuteJitFunction(const CallFrame &frame, const std::string &fnName)
{
RUN(T, ret =, 0);
auto prevCallFrame = POP_CALL_FRAME();
SET_STACK_TOP(prevCallFrame->slot - 1);
PUSH(ret);
}
template <>
inline void VM::ExecuteJitFunction<void>(const CallFrame &frame, const std::string &fnName)
{
RUN(void, , );
auto prevCallFrame = POP_CALL_FRAME();
SET_STACK_TOP(prevCallFrame->slot - 1);
}