-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrb_comp.opt.c
194 lines (159 loc) · 4.84 KB
/
rb_comp.opt.c
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
/* -MODULE----------------------------------------------------------------------
RapidBATCH
Copyright (C) 2008, 2009 by Phorward Software Technologies, Jan Max Meyer
http://www.phorward-software.com ++ [email protected]
File: rb_comp.codegen.c
Author: Jan Max Meyer
Usage: Virtual machine code optimizer and compile-time code execution
----------------------------------------------------------------------------- */
/*
* Includes
*/
#include "rb_global.h"
/*
* Global variables
*/
/*
* Functions
*/
/* -FUNCTION--------------------------------------------------------------------
Function: rb_comp_opt_expr()
Author: Jan Max Meyer
Usage: Optimizes an expression by running it in the virtual
machine. The function will immediatelly patch the virtual
machine program if the executed code results in a
correct result-value.
WARNING: This function must be used very carefully from
within the compiler; Only code-parts with
entirely constant operators can be executed.
If wrong code is passed, it may occur that the
virtual machine crashes.
Parameters: vm_prog* prog Program in which optimization
should be performed
vm_code instr The command which should be
allocated (including a possible
parameter)
pboolean with_return TRUE: Expression returns a value
FALSE: If not!
Returns: int 0 on success
RB_ERR... on failure
~~~ CHANGES & NOTES ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Date: Author: Note:
----------------------------------------------------------------------------- */
int rb_comp_opt_expr( vm_prog* prog, vm_addr begin_opt, pboolean with_return )
{
vm_stack stack;
vm_stackitem item;
vm_code* exec;
vm_addr size;
PROC( "rb_comp_opt_expr" );
PARMS( "prog", "%p", prog );
PARMS( "begin_opt", "%ld", begin_opt );
PARMS( "with_return", "%d", with_return );
size = prog->code_next - begin_opt;
VARS( "size", "%ld", size );
/* Copy code into temporary virtual machine */
MSG( "Creating temporary virtual machine for execution" );
if( !( exec = (vm_code*)pmalloc( size * sizeof( vm_code ) ) ) )
{
MSG( "Failed to reserve temporary virtual machine" );
OUTOFMEM;
}
memcpy( exec, prog->code + begin_opt,
( prog->code_next - begin_opt ) * sizeof( vm_code ) );
MSG( "Resizing virtual machine" );
memset( prog->code + begin_opt, 0,
( prog->code_next - begin_opt ) * sizeof( vm_code ) );
prog->code_next = begin_opt;
/* Run this machine! */
if( rb_vm_run( &stack, exec, size ) < 0 )
{
MSG( "Fatal error on virtual machine execution!" );
pfree( exec );
RETURN( -1 );
}
/* Use return value as optimization result? */
if( with_return )
{
MSG( "Last stack element value is reused" );
rb_vm_stack_pop( &stack, &item );
if( stack.stack_top > 0 )
{
MSG( "Fatal error, stack contains too many values!" );
pfree( exec );
RETURN( -1 );
}
rb_comp_push_constant( ITEM_VAL_GET_STR( &item ) );
rb_vm_item_free( &item );
}
pfree( exec );
RETURN( 0 );
}
/* -FUNCTION--------------------------------------------------------------------
Function: rb_comp_opt_test()
Author: Jan Max Meyer
Usage: Tests a code segment if it can be optimized; Optimization
(and therefore compile-time execution of code!) is not
possible if variable access is performed.
Parameters: vm_code* code Pointer to code where to
test from
vm_addr code_cnt End of code
Returns: pboolean TRUE: If optimization can be
performed
FALSE: If there are instructions
used which can't be
handled at compile-time
~~~ CHANGES & NOTES ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Date: Author: Note:
----------------------------------------------------------------------------- */
pboolean rb_comp_opt_test( vm_code* code, vm_addr code_cnt )
{
vm_code* cp;
PROC( "rb_comp_opt_test" );
PARMS( "code", "%p", code );
PARMS( "code_cnt", "%ld", code_cnt );
for( cp = code; cp < code + code_cnt; cp++ )
{
switch( *cp )
{
/*
When this code is build-up
only of these instructions,
than it can be ran at compile-time
*/
case VMC_NOP:
case VMC_PUSHINT:
case VMC_PUSHADR:
case VMC_PUSHCHR:
case VMC_PUSHDBL:
case VMC_PUSHSTR:
case VMC_NCALL:
case VMC_DROP:
case VMC_JOIN:
case VMC_ADD:
case VMC_SUB:
case VMC_DIV:
case VMC_MUL:
case VMC_MOD:
case VMC_NOT:
case VMC_EQU:
case VMC_NEQ:
case VMC_GRT:
case VMC_LWR:
case VMC_GRE:
case VMC_LWE:
case VMC_NEG:
case VMC_AND:
case VMC_OR:
case VMC_XOR:
case VMC_LAND:
case VMC_LOR:
break;
default:
MSG( "This instruction can't be used at compile-time" );
RETURN( FALSE );
}
cp += VM_GET_PARAM_SIZE( *cp );
}
RETURN( TRUE );
}