[vmkit-commits] [vmkit] r121637 - in /vmkit/branches/py-vm/lib/p3/VMCore: Makefile P3.cpp P3Error.h P3Extractor.cpp P3Extractor.h P3Interpretor.cpp P3Interpretor.h P3Object.cpp P3Object.h opcode.h
Gael Thomas
gael.thomas at lip6.fr
Sun Dec 12 10:42:07 PST 2010
Author: gthomas
Date: Sun Dec 12 12:42:07 2010
New Revision: 121637
URL: http://llvm.org/viewvc/llvm-project?rev=121637&view=rev
Log:
able to interpret a simple helloworld. Object model not yet defined:)
Added:
vmkit/branches/py-vm/lib/p3/VMCore/P3Interpretor.cpp
vmkit/branches/py-vm/lib/p3/VMCore/P3Interpretor.h
vmkit/branches/py-vm/lib/p3/VMCore/P3Object.cpp
vmkit/branches/py-vm/lib/p3/VMCore/P3Object.h
vmkit/branches/py-vm/lib/p3/VMCore/opcode.h
Modified:
vmkit/branches/py-vm/lib/p3/VMCore/Makefile
vmkit/branches/py-vm/lib/p3/VMCore/P3.cpp
vmkit/branches/py-vm/lib/p3/VMCore/P3Error.h
vmkit/branches/py-vm/lib/p3/VMCore/P3Extractor.cpp
vmkit/branches/py-vm/lib/p3/VMCore/P3Extractor.h
Modified: vmkit/branches/py-vm/lib/p3/VMCore/Makefile
URL: http://llvm.org/viewvc/llvm-project/vmkit/branches/py-vm/lib/p3/VMCore/Makefile?rev=121637&r1=121636&r2=121637&view=diff
==============================================================================
--- vmkit/branches/py-vm/lib/p3/VMCore/Makefile (original)
+++ vmkit/branches/py-vm/lib/p3/VMCore/Makefile Sun Dec 12 12:42:07 2010
@@ -10,6 +10,8 @@
include $(LEVEL)/Makefile.config
+NO_PEDANTIC=1
+
ifeq ($(WITH_LLVM_GCC), 1)
MODULE_NAME = P3
else
Modified: vmkit/branches/py-vm/lib/p3/VMCore/P3.cpp
URL: http://llvm.org/viewvc/llvm-project/vmkit/branches/py-vm/lib/p3/VMCore/P3.cpp?rev=121637&r1=121636&r2=121637&view=diff
==============================================================================
--- vmkit/branches/py-vm/lib/p3/VMCore/P3.cpp (original)
+++ vmkit/branches/py-vm/lib/p3/VMCore/P3.cpp Sun Dec 12 12:42:07 2010
@@ -2,6 +2,8 @@
#include "P3Error.h"
#include "P3Reader.h"
#include "P3Extractor.h"
+#include "P3Object.h"
+#include "P3Interpretor.h"
using namespace p3;
@@ -22,14 +24,17 @@
fatal("unable to open: %s", fileName);
P3Reader reader(bc);
- uint16 ver = reader.readU2();
- printf("ver: 0x%x\n", ver);
- uint16 magic = reader.readU2();
- printf("magic: 0x%x\n", magic);
- reader.readU4(); // last modification
+ reader.readU2(); // version 0xf2d1 (???)
+ reader.readU2(); // magic 0xa0d
+ reader.readU4(); // last modification
P3Extractor extractor(&reader);
- extractor.readObject();
+ P3Object* obj = extractor.readObject();
+
+ if(!obj->isCode())
+ fatal("%s does not contain a code", fileName);
+
+ (new P3Interpretor(obj->asCode()))->execute();
}
void P3::runApplicationImpl(int argc, char** argv) {
Modified: vmkit/branches/py-vm/lib/p3/VMCore/P3Error.h
URL: http://llvm.org/viewvc/llvm-project/vmkit/branches/py-vm/lib/p3/VMCore/P3Error.h?rev=121637&r1=121636&r2=121637&view=diff
==============================================================================
--- vmkit/branches/py-vm/lib/p3/VMCore/P3Error.h (original)
+++ vmkit/branches/py-vm/lib/p3/VMCore/P3Error.h Sun Dec 12 12:42:07 2010
@@ -1,6 +1,9 @@
#ifndef _P3_ERROR_H_
#define _P3_ERROR_H_
+#include <stdio.h>
+#include <stdlib.h>
+
#define NI() \
do { fprintf(stderr, "%s is not implemented at %s::%d\n", __PRETTY_FUNCTION__, __FILE__, __LINE__); abort(); } while(0)
Modified: vmkit/branches/py-vm/lib/p3/VMCore/P3Extractor.cpp
URL: http://llvm.org/viewvc/llvm-project/vmkit/branches/py-vm/lib/p3/VMCore/P3Extractor.cpp?rev=121637&r1=121636&r2=121637&view=diff
==============================================================================
--- vmkit/branches/py-vm/lib/p3/VMCore/P3Extractor.cpp (original)
+++ vmkit/branches/py-vm/lib/p3/VMCore/P3Extractor.cpp Sun Dec 12 12:42:07 2010
@@ -1,6 +1,7 @@
#include "P3Extractor.h"
#include "P3Reader.h"
#include "P3Error.h"
+#include "P3Object.h"
using namespace p3;
@@ -29,57 +30,53 @@
#define TYPE_SET '<'
#define TYPE_FROZENSET '>'
-void P3Extractor::readObject() {
+P3Object* P3Extractor::readObject() {
uint8 type = reader->readU1();
- printf("reading object with type: %d (%c)\n", type, type);
-
switch(type) {
case TYPE_NONE:
- break;
+ return new P3None();
case TYPE_TUPLE:
{
uint32 length = reader->readU4();
- printf(" tuple length: %d\n", length);
+ P3Tuple* res = new P3Tuple(length);
for(uint32 i=0; i<length; i++)
- readObject();
+ res->content[i] = readObject();
+ return res;
}
- break;
case TYPE_INTERNED:
case TYPE_STRING:
{
uint32 length = reader->readU4();
- printf(" string length: %d\n", length);
+ P3String* res = new P3String(length);
if(length > INT_MAX)
fatal("wrong length for string");
- for(uint32 i=0; i<length; i++) {
- uint8 c = reader->readU1();
- printf(" %-3d (%c) ", c, c);
- }
- printf("\n");
+ for(uint32 i=0; i<length; i++)
+ res->content[i] = reader->readU1();
+ return res;
}
- break;
case TYPE_CODE:
{
- uint32 nargs = reader->readU4();
- uint32 nlocals = reader->readU4();
- uint32 ssize = reader->readU4();
- uint32 flags = reader->readU4();
- readObject(); // code
- readObject(); // const
- readObject(); // names
- readObject(); // varnames
- readObject(); // freevars
- readObject(); // cellvars
- readObject(); // filename
- readObject(); // name
- reader->readU4(); // line num
- readObject(); // lnotab
+ P3Code* res = new P3Code();
+ res->py_nargs = reader->readU4();
+ res->py_nlocals = reader->readU4();
+ res->py_nstacks = reader->readU4();
+ res->py_flag = reader->readU4();
+ res->py_code = readObject()->asString();
+ res->py_const = readObject()->asTuple();
+ res->py_names = readObject();
+ res->py_varnames = readObject();
+ res->py_freevars = readObject();
+ res->py_cellvars = readObject();
+ res->py_filename = readObject();
+ res->py_name = readObject();
+ res->py_linenum = reader->readU4();
+ res->py_lnotab = readObject();
+ return res;
}
- break;
default:
fatal("wrong type info: %d ('%c')", type, type);
Modified: vmkit/branches/py-vm/lib/p3/VMCore/P3Extractor.h
URL: http://llvm.org/viewvc/llvm-project/vmkit/branches/py-vm/lib/p3/VMCore/P3Extractor.h?rev=121637&r1=121636&r2=121637&view=diff
==============================================================================
--- vmkit/branches/py-vm/lib/p3/VMCore/P3Extractor.h (original)
+++ vmkit/branches/py-vm/lib/p3/VMCore/P3Extractor.h Sun Dec 12 12:42:07 2010
@@ -3,6 +3,7 @@
namespace p3 {
+class P3Object;
class P3Reader;
class P3Extractor {
@@ -14,7 +15,7 @@
/// readObject - unmarshal an object
///
- void readObject();
+ P3Object* readObject();
};
} // namespace p3
Added: vmkit/branches/py-vm/lib/p3/VMCore/P3Interpretor.cpp
URL: http://llvm.org/viewvc/llvm-project/vmkit/branches/py-vm/lib/p3/VMCore/P3Interpretor.cpp?rev=121637&view=auto
==============================================================================
--- vmkit/branches/py-vm/lib/p3/VMCore/P3Interpretor.cpp (added)
+++ vmkit/branches/py-vm/lib/p3/VMCore/P3Interpretor.cpp Sun Dec 12 12:42:07 2010
@@ -0,0 +1,395 @@
+#include "P3Interpretor.h"
+#include "P3Object.h"
+
+#include "opcode.h"
+
+using namespace p3;
+
+#define fetch_arg() (pc+=2, bc->content[pc-1]<<8+bc->content[pc-2])
+#define push(val) stack->content[stack->length++]=val
+#define pop() stack->content[--stack->length]
+
+P3Object* P3Interpretor::execute() {
+ printf("Execute: ");
+ code->print();
+ printf("\n");
+ P3Stack* stack = new P3Stack(code->py_nstacks);
+ P3String* bc = code->py_code;
+ uint32 pc=0;
+
+ while(1) {
+ uint8 op = bc->content[pc++];
+
+ printf(" -- stack: ");
+ stack->print();
+ printf("\n");
+ printf(" -- fetch opcode %d (%s)\n", op, opcodeNames[op]);
+
+ switch(op) {
+ case STOP_CODE: //0
+ case POP_TOP: //1
+ case ROT_TWO: //2
+ case ROT_THREE: //3
+ case DUP_TOP: //4
+ case ROT_FOUR: //5
+ case NOP: //9
+
+ case UNARY_POSITIVE: //10
+ case UNARY_NEGATIVE: //11
+ case UNARY_NOT: //12
+ case UNARY_CONVERT: //13
+
+ case UNARY_INVERT: //15
+
+ case LIST_APPEND: //18
+ case BINARY_POWER: //19
+
+ case BINARY_MULTIPLY: //20
+ case BINARY_DIVIDE: //21
+ case BINARY_MODULO: //22
+ case BINARY_ADD: //23
+ case BINARY_SUBTRACT: //24
+ case BINARY_SUBSCR: //25
+ case BINARY_FLOOR_DIVIDE: //26
+ case BINARY_TRUE_DIVIDE: //27
+ case INPLACE_FLOOR_DIVIDE: //28
+ case INPLACE_TRUE_DIVIDE: //29
+
+ case SLICE: //30
+ /* Also uses : 31-33 */
+
+ case STORE_SLICE: //40
+ /* Also uses : 41-43 */
+
+ case DELETE_SLICE: //50
+ /* Also uses : 51-53 */
+
+ case STORE_MAP: //54
+ case INPLACE_ADD: //55
+ case INPLACE_SUBTRACT: //56
+ case INPLACE_MULTIPLY: //57
+ case INPLACE_DIVIDE: //58
+ case INPLACE_MODULO: //59
+ case STORE_SUBSCR: //60
+ case DELETE_SUBSCR: //61
+
+ case BINARY_LSHIFT: //62
+ case BINARY_RSHIFT: //63
+ case BINARY_AND: //64
+ case BINARY_XOR: //65
+ case BINARY_OR: //66
+ case INPLACE_POWER: //67
+ case GET_ITER: //68
+
+ case PRINT_EXPR: //70
+ NI();
+
+ case PRINT_ITEM: //71
+ pop()->print();
+ break;
+
+ case PRINT_NEWLINE: //72
+ printf("\n");
+ break;
+
+ case PRINT_ITEM_TO: //73
+ case PRINT_NEWLINE_TO: //74
+ case INPLACE_LSHIFT: //75
+ case INPLACE_RSHIFT: //76
+ case INPLACE_AND: //77
+ case INPLACE_XOR: //78
+ case INPLACE_OR: //79
+ case BREAK_LOOP: //80
+ case WITH_CLEANUP: //81
+ case LOAD_LOCALS: //82
+ NI();
+
+ case RETURN_VALUE: //83
+ return pop();
+
+ case IMPORT_STAR: //84
+ case EXEC_STMT: //85
+ case YIELD_VALUE: //86
+ case POP_BLOCK: //87
+ case END_FINALLY: //88
+ case BUILD_CLASS: //89
+
+ case HAVE_ARGUMENT: //90
+ /* Opcodes from here have an argument: */
+
+ // case STORE_NAME: //90
+ /* Index in name list */
+ case DELETE_NAME: //91
+ /* "" */
+ case UNPACK_SEQUENCE: //92
+ /* Number of sequence items */
+ case FOR_ITER: //93
+
+ case STORE_ATTR: //95
+ /* Index in name list */
+ case DELETE_ATTR: //96
+ /* "" */
+ case STORE_GLOBAL: //97
+ /* "" */
+ case DELETE_GLOBAL: //98
+ /* "" */
+ case DUP_TOPX: //99
+ /* number of items to duplicate */
+ NI();
+
+ case LOAD_CONST: // 100
+ push(code->py_const->at(fetch_arg()));
+ break;
+
+ /* Index in const list */
+ case LOAD_NAME: //101
+ /* Index in name list */
+ case BUILD_TUPLE: //102
+ /* Number of tuple items */
+ case BUILD_LIST: //103
+ /* Number of list items */
+ case BUILD_MAP: //104
+ /* Always zero for now */
+ case LOAD_ATTR: //105
+ /* Index in name list */
+ case COMPARE_OP: //106
+ /* Comparison operator */
+ case IMPORT_NAME: //107
+ /* Index in name list */
+ case IMPORT_FROM: //108
+ /* Index in name list */
+
+ case JUMP_FORWARD: //110
+ /* Number of bytes to skip */
+ case JUMP_IF_FALSE: //111
+ /* "" */
+ case JUMP_IF_TRUE: //112
+ /* "" */
+ case JUMP_ABSOLUTE: //113
+ /* Target byte offset from beginning of code */
+
+ case LOAD_GLOBAL: //116
+ /* Index in name list */
+
+ case CONTINUE_LOOP: //119
+ /* Start of loop (absolute) */
+ case SETUP_LOOP: //120
+ /* Target address (relative) */
+ case SETUP_EXCEPT: //121
+ /* "" */
+ case SETUP_FINALLY: //122
+ /* "" */
+
+ case LOAD_FAST: //124
+ /* Local variable number */
+ case STORE_FAST: //125
+ /* Local variable number */
+ case DELETE_FAST: //126
+ /* Local variable number */
+
+ case RAISE_VARARGS: //130
+ /* Number of raise arguments (1, 2 or 3) */
+ /* CALL_FUNCTION_XXX opcodes defined below depend on this definition */
+ case CALL_FUNCTION: //131
+ /* #args + (#kwargs<<8) */
+ case MAKE_FUNCTION: //132
+ /* #defaults */
+ case BUILD_SLICE: //133
+ /* Number of items */
+
+ case MAKE_CLOSURE: //134
+ /* #free vars */
+ case LOAD_CLOSURE: //135
+ /* Load free variable from closure */
+ case LOAD_DEREF: //136
+ /* Load and dereference from closure cell */
+ case STORE_DEREF: //137
+ /* Store into cell */
+
+ /* The next 3 opcodes must be contiguous and satisfy
+ (CALL_FUNCTION_VAR - CALL_FUNCTION) & 3 == 1 */
+ case CALL_FUNCTION_VAR: //140
+ /* #args + (#kwargs<<8) */
+ case CALL_FUNCTION_KW: //141
+ /* #args + (#kwargs<<8) */
+ case CALL_FUNCTION_VAR_KW: //142
+ /* #args + (#kwargs<<8) */
+
+ /* Support for opargs more than : //16 bits long */
+ case EXTENDED_ARG: //143
+
+ default:
+ fatal("unable to interpret opcode: %d", op);
+
+ }
+ }
+}
+
+const char* P3Interpretor::opcodeNames[] = {
+ "STOP_CODE", // 0
+ "POP_TOP", // 1
+ "ROT_TWO", // 2
+ "ROT_THREE", // 3
+ "DUP_TOP", // 4
+ "ROT_FOUR", // 5
+ "6__unknow", // 6
+ "7__unknow", // 7
+ "8__unknow", // 8
+ "NOP", // 9
+
+ "UNARY_POSITIVE", // 10
+ "UNARY_NEGATIVE", // 11
+ "UNARY_NOT", // 12
+ "UNARY_CONVERT", // 13
+ "14__unknow", // 14
+ "UNARY_INVERT", // 15
+ "16__unknow", // 16
+ "17__unknow", // 17
+ "LIST_APPEND", // 18
+ "BINARY_POWER", // 19
+
+ "BINARY_MULTIPLY", // 20
+ "BINARY_DIVIDE", // 21
+ "BINARY_MODULO", // 22
+ "BINARY_ADD", // 23
+ "BINARY_SUBTRACT", // 24
+ "BINARY_SUBSCR", // 25
+ "BINARY_FLOOR_DIVIDE", // 26
+ "BINARY_TRUE_DIVIDE", // 27
+ "INPLACE_FLOOR_DIVIDE", // 28
+ "INPLACE_TRUE_DIVIDE", // 29
+
+ "SLICE", // 30
+ /* Also uses , // 31-, // 33 */
+ "31__SLICE", // 31
+ "32__SLICE", // 32
+ "33__SLICE", // 33
+ "34__unknow", // 34
+ "35__unknow", // 35
+ "36__unknow", // 36
+ "37__unknow", // 37
+ "38__unknow", // 38
+ "39__unknow", // 39
+
+ "STORE_SLICE", // 40
+ /* Also uses , // 41-, // 43 */
+ "41__STORE_SLICE", // 41
+ "42__STORE_SLICE", // 42
+ "43__STORE_SLICE", // 43
+ "44__unknow", // 44
+ "45__unknow", // 45
+ "46__unknow", // 46
+ "47__unknow", // 47
+ "48__unknow", // 48
+ "49__unknow", // 49
+
+ "DELETE_SLICE", // 50
+ /* Also uses , // 51-, // 53 */
+ "51__DELETE_SLICE", // 51
+ "52__DELETE_SLICE", // 52
+ "53__DELETE_SLICE", // 53
+ "STORE_MAP", // 54
+ "INPLACE_ADD", // 55
+ "INPLACE_SUBTRACT", // 56
+ "INPLACE_MULTIPLY", // 57
+ "INPLACE_DIVIDE", // 58
+ "INPLACE_MODULO", // 59
+
+ "STORE_SUBSCR", // 60
+ "DELETE_SUBSCR", // 61
+ "BINARY_LSHIFT", // 62
+ "BINARY_RSHIFT", // 63
+ "BINARY_AND", // 64
+ "BINARY_XOR", // 65
+ "BINARY_OR", // 66
+ "INPLACE_POWER", // 67
+ "GET_ITER", // 68
+ "69__unknow", // 69
+
+ "PRINT_EXPR", // 70
+ "PRINT_ITEM", // 71
+ "PRINT_NEWLINE", // 72
+ "PRINT_ITEM_TO", // 73
+ "PRINT_NEWLINE_TO", // 74
+ "INPLACE_LSHIFT", // 75
+ "INPLACE_RSHIFT", // 76
+ "INPLACE_AND", // 77
+ "INPLACE_XOR", // 78
+ "INPLACE_OR", // 79
+
+ "BREAK_LOOP", // 80
+ "WITH_CLEANUP", // 81
+ "LOAD_LOCALS", // 82
+ "RETURN_VALUE", // 83
+ "IMPORT_STAR", // 84
+ "EXEC_STMT", // 85
+ "YIELD_VALUE", // 86
+ "POP_BLOCK", // 87
+ "END_FINALLY", // 88
+ "BUILD_CLASS", // 89
+
+ "HAVE_ARGUMENT", // 90 /* Opcodes from here have an argument: */
+ // "STORE_NAME", // 90 /* Index in name list */
+ "DELETE_NAME", // 91 /* "", */
+ "UNPACK_SEQUENCE", // 92 /* Number of sequence items */
+ "FOR_ITER", // 93
+ "94__unknow", // 94
+ "STORE_ATTR", // 95 /* Index in name list */
+ "DELETE_ATTR", // 96 /* "" */
+ "STORE_GLOBAL", // 97 /* "" */
+ "DELETE_GLOBAL", // 98 /* "" */
+ "DUP_TOPX", // 99 /* number of items to duplicate */
+
+ "LOAD_CONST", // 100 /* Index in const list */
+ "LOAD_NAME", // 101 /* Index in name list */
+ "BUILD_TUPLE", // 102 /* Number of tuple items */
+ "BUILD_LIST", // 103 /* Number of list items */
+ "BUILD_MAP", // 104 /* Always zero for now */
+ "LOAD_ATTR", // 105 /* Index in name list */
+ "COMPARE_OP", // 106 /* Comparison operator */
+ "IMPORT_NAME", // 107 /* Index in name list */
+ "IMPORT_FROM", // 108 /* Index in name list */
+ "109__unknow", // 109
+
+ "JUMP_FORWARD", // 110 /* Number of bytes to skip */
+ "JUMP_IF_FALSE", // 111 /* "" */
+ "JUMP_IF_TRUE", // 112 /* "" */
+ "JUMP_ABSOLUTE", // 113 /* Target byte offset from beginning of code */
+ "114__unknow", // 114
+ "115__unknow", // 115
+ "LOAD_GLOBAL", // 116 /* Index in name list */
+ "117__unknow", // 117
+ "118__unknow", // 118
+ "CONTINUE_LOOP", // 119 /* Start of loop (absolute) */
+
+ "SETUP_LOOP", // 120 /* Target address (relative) */
+ "SETUP_EXCEPT", // 121 /* "" */
+ "SETUP_FINALLY", // 122 /* "" */
+ "123__unknow", // 123
+ "LOAD_FAST", // 124 /* Local variable number */
+ "STORE_FAST", // 125 /* Local variable number */
+ "DELETE_FAST", // 126 /* Local variable number */
+ "127__unknow", // 127
+ "128__unknow", // 128
+ "129__unknow", // 129
+
+ "RAISE_VARARGS", // 130 /* Number of raise arguments (, // 1, , // 2 or , // 3) */
+ /* CALL_FUNCTION_XXX opcodes defined below depend on this definition */
+ "CALL_FUNCTION", // 131 /* #args + (#kwargs<<, // 8) */
+ "MAKE_FUNCTION", // 132 /* #defaults */
+ "BUILD_SLICE", // 133 /* Number of items */
+ "MAKE_CLOSURE", // 134 /* #free vars */
+ "LOAD_CLOSURE", // 135 /* Load free variable from closure */
+ "LOAD_DEREF", // 136 /* Load and dereference from closure cell */
+ "STORE_DEREF", // 137 /* Store into cell */
+ "138__unknow", // 138
+ "139__unknow", // 139
+
+ /* The next , // 3 opcodes must be contiguous and satisfy
+ (CALL_FUNCTION_VAR - CALL_FUNCTION) & == 1 */
+ "CALL_FUNCTION_VAR", // 140 /* #args + (#kwargs<<8) */
+ "CALL_FUNCTION_KW", // 141 /* #args + (#kwargs<<, // 8) */
+ "CALL_FUNCTION_VAR_KW", // 142 /* #args + (#kwargs<<, // 8) */
+ /* Support for opargs more than 16 bits long */
+ "EXTENDED_ARG", // 143
+};
Added: vmkit/branches/py-vm/lib/p3/VMCore/P3Interpretor.h
URL: http://llvm.org/viewvc/llvm-project/vmkit/branches/py-vm/lib/p3/VMCore/P3Interpretor.h?rev=121637&view=auto
==============================================================================
--- vmkit/branches/py-vm/lib/p3/VMCore/P3Interpretor.h (added)
+++ vmkit/branches/py-vm/lib/p3/VMCore/P3Interpretor.h Sun Dec 12 12:42:07 2010
@@ -0,0 +1,21 @@
+#ifndef _P3_INTERPRETOR_H_
+#define _P3_INTERPRETOR_H_
+
+namespace p3 {
+
+class P3Code;
+class P3Object;
+
+class P3Interpretor {
+ static const char *opcodeNames[];
+ P3Code* code;
+
+public:
+ P3Interpretor(P3Code* c) { this->code = c; }
+
+ P3Object* execute();
+};
+
+} // namespace p3
+
+#endif
Added: vmkit/branches/py-vm/lib/p3/VMCore/P3Object.cpp
URL: http://llvm.org/viewvc/llvm-project/vmkit/branches/py-vm/lib/p3/VMCore/P3Object.cpp?rev=121637&view=auto
==============================================================================
--- vmkit/branches/py-vm/lib/p3/VMCore/P3Object.cpp (added)
+++ vmkit/branches/py-vm/lib/p3/VMCore/P3Object.cpp Sun Dec 12 12:42:07 2010
@@ -0,0 +1,14 @@
+#include "P3Object.h"
+
+using namespace p3;
+
+P3String::P3String(uint32 l) {
+ content = new uint8[l+1];
+ length = l;
+ content[l] = 0;
+}
+
+P3Tuple::P3Tuple(uint32 l) {
+ content = new P3Object*[l];
+ length = l;
+}
Added: vmkit/branches/py-vm/lib/p3/VMCore/P3Object.h
URL: http://llvm.org/viewvc/llvm-project/vmkit/branches/py-vm/lib/p3/VMCore/P3Object.h?rev=121637&view=auto
==============================================================================
--- vmkit/branches/py-vm/lib/p3/VMCore/P3Object.h (added)
+++ vmkit/branches/py-vm/lib/p3/VMCore/P3Object.h Sun Dec 12 12:42:07 2010
@@ -0,0 +1,97 @@
+#ifndef _P3_OBJECT_H_
+#define _P3_OBJECT_H_
+
+#include <types.h>
+#include <stdint.h>
+
+#include "P3Error.h"
+
+namespace p3 {
+
+class P3String;
+class P3Tuple;
+class P3Code;
+class P3None;
+
+class P3Object {
+public:
+#define do_type(type, name) \
+ virtual bool is##name() { return 0; } \
+ type* as##name() { if(!is##name()) fatal("%p is not a "#name, (void*)this); return (type*)this; }
+
+ do_type(P3String, String)
+ do_type(P3Tuple, Tuple)
+ do_type(P3None, None)
+ do_type(P3Code, Code)
+
+#undef do_type
+
+ virtual void print() { printf("object@%p", (void*)this); }
+};
+
+class P3None : public P3Object {
+public:
+ virtual void print() { printf("<none>"); }
+};
+
+class P3String : public P3Object {
+public:
+ uint32 length;
+ uint8* content;
+
+ P3String(uint32 n);
+
+ virtual bool isString() { return 1; }
+ virtual void print() { printf("%s", content); }
+};
+
+class P3Tuple : public P3Object {
+public:
+ uint32 length;
+ P3Object** content;
+
+ P3Tuple(uint32 n);
+
+ P3Object* at(uint32 idx) {
+ if(idx >= length)
+ fatal("array out of bound: %d", idx);
+ return content[idx];
+ }
+
+ virtual bool isTuple() { return 1; }
+
+ virtual void print() { printf("["); for(uint32 i=0; i<length; i++) content[i]->print(); printf("]"); }
+};
+
+class P3Stack : public P3Tuple {
+public:
+ uint32 capacity;
+
+ P3Stack(uint32 c) : P3Tuple(c) { length = 0; capacity = c; }
+};
+
+class P3Code : public P3Object {
+public:
+ uint32 py_nargs;
+ uint32 py_nlocals;
+ uint32 py_nstacks;
+ uint32 py_flag;
+ P3String* py_code;
+ P3Tuple* py_const;
+ P3Object* py_names;
+ P3Object* py_varnames;
+ P3Object* py_freevars;
+ P3Object* py_cellvars;
+ P3Object* py_filename;
+ P3Object* py_name;
+ uint32 py_linenum;
+ P3Object* py_lnotab;
+
+ virtual bool isCode() { return 1; }
+
+ virtual void print() { printf("Code<"); py_name->print(); printf(">"); }
+};
+
+} // namespace p3
+
+#endif
Added: vmkit/branches/py-vm/lib/p3/VMCore/opcode.h
URL: http://llvm.org/viewvc/llvm-project/vmkit/branches/py-vm/lib/p3/VMCore/opcode.h?rev=121637&view=auto
==============================================================================
--- vmkit/branches/py-vm/lib/p3/VMCore/opcode.h (added)
+++ vmkit/branches/py-vm/lib/p3/VMCore/opcode.h Sun Dec 12 12:42:07 2010
@@ -0,0 +1,161 @@
+#ifndef Py_OPCODE_H
+#define Py_OPCODE_H
+
+#define Py_LT 0
+#define Py_LE 1
+#define Py_EQ 2
+#define Py_NE 3
+#define Py_GT 4
+#define Py_GE 5
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+
+/* Instruction opcodes for compiled code */
+
+#define STOP_CODE 0
+#define POP_TOP 1
+#define ROT_TWO 2
+#define ROT_THREE 3
+#define DUP_TOP 4
+#define ROT_FOUR 5
+#define NOP 9
+
+#define UNARY_POSITIVE 10
+#define UNARY_NEGATIVE 11
+#define UNARY_NOT 12
+#define UNARY_CONVERT 13
+
+#define UNARY_INVERT 15
+
+#define LIST_APPEND 18
+#define BINARY_POWER 19
+
+#define BINARY_MULTIPLY 20
+#define BINARY_DIVIDE 21
+#define BINARY_MODULO 22
+#define BINARY_ADD 23
+#define BINARY_SUBTRACT 24
+#define BINARY_SUBSCR 25
+#define BINARY_FLOOR_DIVIDE 26
+#define BINARY_TRUE_DIVIDE 27
+#define INPLACE_FLOOR_DIVIDE 28
+#define INPLACE_TRUE_DIVIDE 29
+
+#define SLICE 30
+/* Also uses 31-33 */
+
+#define STORE_SLICE 40
+/* Also uses 41-43 */
+
+#define DELETE_SLICE 50
+/* Also uses 51-53 */
+
+#define STORE_MAP 54
+#define INPLACE_ADD 55
+#define INPLACE_SUBTRACT 56
+#define INPLACE_MULTIPLY 57
+#define INPLACE_DIVIDE 58
+#define INPLACE_MODULO 59
+#define STORE_SUBSCR 60
+#define DELETE_SUBSCR 61
+
+#define BINARY_LSHIFT 62
+#define BINARY_RSHIFT 63
+#define BINARY_AND 64
+#define BINARY_XOR 65
+#define BINARY_OR 66
+#define INPLACE_POWER 67
+#define GET_ITER 68
+
+#define PRINT_EXPR 70
+#define PRINT_ITEM 71
+#define PRINT_NEWLINE 72
+#define PRINT_ITEM_TO 73
+#define PRINT_NEWLINE_TO 74
+#define INPLACE_LSHIFT 75
+#define INPLACE_RSHIFT 76
+#define INPLACE_AND 77
+#define INPLACE_XOR 78
+#define INPLACE_OR 79
+#define BREAK_LOOP 80
+#define WITH_CLEANUP 81
+#define LOAD_LOCALS 82
+#define RETURN_VALUE 83
+#define IMPORT_STAR 84
+#define EXEC_STMT 85
+#define YIELD_VALUE 86
+#define POP_BLOCK 87
+#define END_FINALLY 88
+#define BUILD_CLASS 89
+
+#define HAVE_ARGUMENT 90 /* Opcodes from here have an argument: */
+
+#define STORE_NAME 90 /* Index in name list */
+#define DELETE_NAME 91 /* "" */
+#define UNPACK_SEQUENCE 92 /* Number of sequence items */
+#define FOR_ITER 93
+
+#define STORE_ATTR 95 /* Index in name list */
+#define DELETE_ATTR 96 /* "" */
+#define STORE_GLOBAL 97 /* "" */
+#define DELETE_GLOBAL 98 /* "" */
+#define DUP_TOPX 99 /* number of items to duplicate */
+#define LOAD_CONST 100 /* Index in const list */
+#define LOAD_NAME 101 /* Index in name list */
+#define BUILD_TUPLE 102 /* Number of tuple items */
+#define BUILD_LIST 103 /* Number of list items */
+#define BUILD_MAP 104 /* Always zero for now */
+#define LOAD_ATTR 105 /* Index in name list */
+#define COMPARE_OP 106 /* Comparison operator */
+#define IMPORT_NAME 107 /* Index in name list */
+#define IMPORT_FROM 108 /* Index in name list */
+
+#define JUMP_FORWARD 110 /* Number of bytes to skip */
+#define JUMP_IF_FALSE 111 /* "" */
+#define JUMP_IF_TRUE 112 /* "" */
+#define JUMP_ABSOLUTE 113 /* Target byte offset from beginning of code */
+
+#define LOAD_GLOBAL 116 /* Index in name list */
+
+#define CONTINUE_LOOP 119 /* Start of loop (absolute) */
+#define SETUP_LOOP 120 /* Target address (relative) */
+#define SETUP_EXCEPT 121 /* "" */
+#define SETUP_FINALLY 122 /* "" */
+
+#define LOAD_FAST 124 /* Local variable number */
+#define STORE_FAST 125 /* Local variable number */
+#define DELETE_FAST 126 /* Local variable number */
+
+#define RAISE_VARARGS 130 /* Number of raise arguments (1, 2 or 3) */
+/* CALL_FUNCTION_XXX opcodes defined below depend on this definition */
+#define CALL_FUNCTION 131 /* #args + (#kwargs<<8) */
+#define MAKE_FUNCTION 132 /* #defaults */
+#define BUILD_SLICE 133 /* Number of items */
+
+#define MAKE_CLOSURE 134 /* #free vars */
+#define LOAD_CLOSURE 135 /* Load free variable from closure */
+#define LOAD_DEREF 136 /* Load and dereference from closure cell */
+#define STORE_DEREF 137 /* Store into cell */
+
+/* The next 3 opcodes must be contiguous and satisfy
+ (CALL_FUNCTION_VAR - CALL_FUNCTION) & 3 == 1 */
+#define CALL_FUNCTION_VAR 140 /* #args + (#kwargs<<8) */
+#define CALL_FUNCTION_KW 141 /* #args + (#kwargs<<8) */
+#define CALL_FUNCTION_VAR_KW 142 /* #args + (#kwargs<<8) */
+
+/* Support for opargs more than 16 bits long */
+#define EXTENDED_ARG 143
+
+
+enum cmp_op {PyCmp_LT=Py_LT, PyCmp_LE=Py_LE, PyCmp_EQ=Py_EQ, PyCmp_NE=Py_NE, PyCmp_GT=Py_GT, PyCmp_GE=Py_GE,
+ PyCmp_IN, PyCmp_NOT_IN, PyCmp_IS, PyCmp_IS_NOT, PyCmp_EXC_MATCH, PyCmp_BAD};
+
+#define HAS_ARG(op) ((op) >= HAVE_ARGUMENT)
+
+#ifdef __cplusplus
+}
+#endif
+#endif /* !Py_OPCODE_H */
More information about the vmkit-commits
mailing list