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
|
/*
FALCON - The Falcon Programming Language.
FILE: pcode.cpp
Utilities to manage the Falcon Virtual Machine pseudo code.
-------------------------------------------------------------------
Author: Giancarlo Niccolai
Begin: Fri, 24 Jul 2009 19:42:40 +0200
-------------------------------------------------------------------
(C) Copyright 2009: the FALCON developers (see list in AUTHORS file)
See LICENSE file for licensing details.
*/
#include <falcon/pcode.h>
#include <falcon/common.h>
#include <falcon/module.h>
#include <falcon/symbol.h>
namespace Falcon {
void PCODE::convertEndianity( uint32 paramType, byte* targetArea, bool into )
{
switch( paramType )
{
case P_PARAM_INT32:
case P_PARAM_STRID:
case P_PARAM_LBIND:
case P_PARAM_GLOBID:
case P_PARAM_LOCID:
case P_PARAM_PARID:
case P_PARAM_NTD32:
*reinterpret_cast<int32 *>(targetArea) = endianInt32(*reinterpret_cast<int32 *>(targetArea) );
break;
case P_PARAM_INT64:
case P_PARAM_NTD64:
{
// high part - low part
if(into)
{
// load from our straight int
uint64 value64 = loadInt64( targetArea );
// endianize each subpart, and invert their position.
*reinterpret_cast<uint32 *>(targetArea+sizeof(uint32)) = endianInt32((uint32)(value64 >> 32));
*reinterpret_cast<uint32 *>(targetArea) = (uint32) endianInt32((uint32)(value64 & 0xFFFFFFFF));
}
else {
// load from different endianity
uint64 value64 = grabInt64( targetArea );
*reinterpret_cast<uint32 *>(targetArea) = (uint32)(value64 >> 32);
*reinterpret_cast<uint32 *>(targetArea+sizeof(uint32)) = (uint32) value64;
}
}
break;
case P_PARAM_NUM:
{
union t_unumeric {
struct t_integer {
uint32 high;
uint32 low;
} integer;
numeric number;
} unumeric;
unumeric.number = grabNum( targetArea );
// high part - low part
*reinterpret_cast<uint32 *>(targetArea) = unumeric.integer.high;
*reinterpret_cast<uint32 *>(targetArea+sizeof(uint32)) = unumeric.integer.low;
}
break;
}
}
uint32 PCODE::advanceParam( uint32 paramType )
{
uint32 offset;
switch( paramType )
{
case P_PARAM_NIL:
case P_PARAM_NOTUSED:
case P_PARAM_TRUE:
case P_PARAM_FALSE:
case P_PARAM_REGA:
case P_PARAM_REGB:
case P_PARAM_REGS1:
case P_PARAM_FSELF:
case P_PARAM_REGL1:
case P_PARAM_REGL2:
offset = 0;
break;
case P_PARAM_NUM:
case P_PARAM_INT64:
case P_PARAM_NTD64:
offset = 8;
break;
default:
offset = 4;
}
return offset;
}
void PCODE::deendianize( byte* code, uint32 codeSize, bool into )
{
uint32 iPos =0;
byte opcode;
while( iPos < codeSize )
{
opcode = code[ iPos ];
uint32 iStart = iPos;
// get the options
iPos += 4;
if( code[ iStart + 1 ] != 0 )
{
convertEndianity( code[ iStart + 1 ], code + iPos, into );
iPos += advanceParam( code[iStart + 1] );
if( code[ iStart + 2 ] != 0 )
{
convertEndianity( code[iStart + 2], code + iPos );
iPos += advanceParam( code[iStart + 2] );
if( code[ iStart + 3 ] != 0 )
{
convertEndianity( code[iStart + 3], code + iPos );
iPos += advanceParam( code[iStart + 3] );
}
}
}
// if the operation is a switch, it's handled a bit specially.
if ( opcode == P_SWCH || opcode == P_SELE )
{
// get the switch table (aready de-endianized in the above step)
iPos -= sizeof(int64);
uint16 sw_int, sw_rng, sw_str, sw_obj;
uint64 value64;
if ( into )
{
// we have just destroyed our int64 switch value, which must be inverted
value64 = grabInt64( code+iPos );
}
else {
// we have just correctly decoded our integer from the stream.
value64 = loadInt64( code + iPos );
}
sw_int = (int16) (value64 >> 48);
sw_rng = (int16) ((value64 >> 32) & 0xFFFF);
sw_str = (int16) ((value64 >> 16) & 0xFFFF);
sw_obj = (int16) (value64 & 0xFFFF);
iPos += sizeof( uint64 );
// Endianize the nil landing
*reinterpret_cast<uint32 *>(code+iPos) = endianInt32( *reinterpret_cast<uint32 *>(code+iPos) );
iPos += sizeof( uint32 );
// endianize the integer table
while( sw_int > 0 )
{
if( into )
{
// the int64 value
uint64 value64 = loadInt64( code+iPos );
// high part - low part
*reinterpret_cast<uint32 *>(code+iPos+sizeof(uint32)) = endianInt32((uint32)(value64 >> 32));
*reinterpret_cast<uint32 *>(code+iPos) = endianInt32((uint32) value64);
}
else {
uint64 value64 = grabInt64( code+iPos );
*reinterpret_cast<uint32 *>(code+iPos) = (uint32)(value64 >> 32);
*reinterpret_cast<uint32 *>(code+iPos+sizeof(uint32)) = (uint32) value64;
}
iPos += sizeof( int64 );
// and the landing
*reinterpret_cast<int32 *>(code+iPos) = endianInt32( *reinterpret_cast<int32 *>(code+iPos) );
iPos += sizeof( int32 );
--sw_int;
}
// endianize the range table
while( sw_rng > 0 )
{
// the int32 value -- start
*reinterpret_cast<int32 *>(code+iPos) = endianInt32( *reinterpret_cast<int32 *>(code+iPos) );
iPos += sizeof( int32 );
// the int32 value -- end
*reinterpret_cast<int32 *>(code+iPos) = endianInt32( *reinterpret_cast<int32 *>(code+iPos) );
iPos += sizeof( int32 );
// and the landing
*reinterpret_cast<int32 *>(code+iPos) = endianInt32( *reinterpret_cast<int32 *>(code+iPos) );
iPos += sizeof( int32 );
--sw_rng;
}
// endianize the string table
while( sw_str > 0 )
{
// the int32 string index
*reinterpret_cast<int32 *>(code+iPos) = endianInt32( *reinterpret_cast<int32 *>(code+iPos) );
iPos += sizeof( int32 );
// and the landing
*reinterpret_cast<int32 *>(code+iPos) = endianInt32( *reinterpret_cast<int32 *>(code+iPos) );
iPos += sizeof( int32 );
--sw_str;
}
// endianize the object table
while( sw_obj > 0 )
{
// the int32 symbol index
*reinterpret_cast<int32 *>(code+iPos) = endianInt32( *reinterpret_cast<int32 *>(code+iPos) );
iPos += sizeof( int32 );
// and the landing
*reinterpret_cast<int32 *>(code+iPos) = endianInt32( *reinterpret_cast<int32 *>(code+iPos) );
iPos += sizeof( int32 );
--sw_obj;
}
}
}
}
}
/* end of pcode.cpp */
|