Permalink
Browse files

Added tracebacks, proper exception support, lnotab support, vtable ca…

…lling, some logging.
  • Loading branch information...
1 parent 1a6dfe3 commit 069ec8fab2975dcf764325d24af392cd2af9f379 @jmolloy committed Mar 27, 2011
View
@@ -24,6 +24,9 @@ set(SRCS
src/PyNone.cc
src/PyRuntime.cc
src/PyFunction.cc
+ src/Object.cc
+ src/Exception.cc
+ src/Traceback.cc
)
find_library(LIBJIT jit)
View
@@ -28,59 +28,59 @@ class BasicBlock : public Value {
~BasicBlock();
/** Loads the argument as a constant, pushing onto the stack. */
- void LoadConstant(Constant *arg, int &id);
+ void LoadConstant(Constant *arg, int &id, int bytecode_offset);
/** Loads the named global variable, pushing onto the stack. */
- void LoadGlobal(std::string arg, int &id);
+ void LoadGlobal(std::string arg, int &id, int bytecode_offset);
/** Loads the named local (or parameter) variable, pushing onto the stack. */
- void LoadLocal(std::string arg, int &id);
+ void LoadLocal(std::string arg, int &id, int bytecode_offset);
/** Stores TOS to the named global. */
- void StoreGlobal(std::string arg, int &id);
+ void StoreGlobal(std::string arg, int &id, int bytecode_offset);
/** Stores TOS to the named local. */
- void StoreLocal(std::string arg, int &id);
+ void StoreLocal(std::string arg, int &id, int bytecode_offset);
/** Creates a new function or closure, with the given number of default parameters, fished from the stack BEFORE
the code object is expected. */
- void BindClosure(long arg, int &id);
+ void BindClosure(long arg, int &id, int bytecode_offset);
/** Performs a "standard" function call, with the given number of positional arguments and keyword arguments. */
void Call(unsigned char num_positional,
- unsigned char num_keywords, int &id);
+ unsigned char num_keywords, int &id, int bytecode_offset);
/** Pops TOS */
void Pop(int &id);
/** Returns TOS. This is a terminator instruction. */
- void Return(int &id);
+ void Return(int &id, int bytecode_offset);
/** Conditional jump. This is a terminator instruction.
@param alwaysPop If true, Pop the TOS always; else only pop the item if the branch was NOT taken.
@param condition True or false comparison of TOS.
@param trueBranch Block to branch to if the condition succeeds.
@param falseBranch Block to branch to if the condition fails. */
- void ConditionalJump(bool alwaysPop, bool condition, BasicBlock *trueBranch, BasicBlock *falseBranch, int &id);
+ void ConditionalJump(bool alwaysPop, bool condition, BasicBlock *trueBranch, BasicBlock *falseBranch, int &id, int bytecode_offset);
/** Jump to block. This is a terminator instruction. */
- void Jump(BasicBlock *branch, int &id);
+ void Jump(BasicBlock *branch, int &id, int bytecode_offset);
/** Perform a binary operation on TOS and TOS1 */
- void BinaryOp(std::string op, int &id);
+ void BinaryOp(std::string op, int &id, int bytecode_offset);
/** Get an attribute of TOS. */
- void GetAttr(std::string attr, int &id);
+ void GetAttr(std::string attr, int &id, int bytecode_offset);
void Dup();
- void BuildTuple(int n, int &id);
+ void BuildTuple(int n, int &id, int bytecode_offset);
void BeginCatch(int &id);
- void Compare(int op, int &id);
+ void Compare(int op, int &id, int bytecode_offset);
- void PrintItem(int &id);
- void PrintNewline(int &id);
+ void PrintItem(int &id, int bytecode_offset);
+ void PrintNewline(int &id, int bytecode_offset);
- void ReRaise(int &id);
+ void ReRaise(int &id, int bytecode_offset);
void AddPredecessor(BasicBlock *b, int &id);
View
@@ -11,7 +11,7 @@
class Constant : public Value {
public:
static class ConstantBool *GetBool(bool b);
- static class ConstantInt *GetInt(int n);
+ static class ConstantInt *GetInt(long n);
static class ConstantInt64 *GetInt64(int64_t n);
static class ConstantFloat *GetFloat(double n);
static class ConstantString *GetString(const std::string &str);
View
@@ -0,0 +1,38 @@
+#ifndef EXCEPTION_H
+#define EXCEPTION_H
+
+#include <Constant.h>
+#include <Traceback.h>
+
+class Exception : public Constant {
+public:
+ Exception(const std::string &name, const std::string &desc) :
+ m_name(name), m_desc(desc), m_traceback(new Traceback()) {
+ }
+
+ const std::string &GetDesc() {
+ return m_desc;
+ }
+ void SetDesc(std::string &d) {
+ m_desc = d;
+ }
+ const char *what() {
+ return m_desc.c_str();
+ }
+
+ void SetTraceback(Traceback *t) {
+ m_traceback = t;
+ }
+ Traceback *GetTraceback() {
+ return m_traceback;
+ }
+
+ virtual const std::string Repr();
+
+protected:
+ std::string m_name;
+ std::string m_desc;
+ Traceback *m_traceback;
+};
+
+#endif
View
@@ -17,7 +17,7 @@ class Function : public Constant {
public:
/** Creates a new function with the given identifying name. */
Function(std::string name, Code *code, Module *module);
- ~Function();
+ virtual ~Function();
/** Returns the entry block of this function. */
BasicBlock *GetEntryBlock() {
@@ -44,6 +44,8 @@ class Function : public Constant {
jit_function_t LJ_Codegen(jit_context_t ctx=0);
+ virtual void *GetFnPtr();
+
private:
/** Entry block */
BasicBlock *m_entry_block;
@@ -65,4 +67,21 @@ class Function : public Constant {
jit_function_t m_jit_function;
};
+class BuiltinFunction : public Function {
+public:
+ BuiltinFunction(void *fn) :
+ Function("<builtin>", NULL, NULL),
+ m_fn(fn) {
+ }
+
+ virtual void *GetFnPtr() {
+ return m_fn;
+ }
+
+ virtual const std::string Repr();
+
+private:
+ void *m_fn;
+};
+
#endif
@@ -22,7 +22,6 @@ class Instruction : public Value {
const char *m_mnemonic;
std::vector<Value*> m_args;
-
};
#endif
View
@@ -3,7 +3,7 @@
#include <string>
#include <Function.h>
-#include <PyDict.h>
+#include <variables.h>
/** A container class for module-level functions and classes. */
class Module {
@@ -19,7 +19,7 @@ class Module {
return m_name;
}
- FPyDict *GetGlobals() {
+ Dict *GetGlobals() {
return m_globals;
}
@@ -31,7 +31,7 @@ class Module {
std::string m_name;
Function *m_main;
- FPyDict *m_globals;
+ Dict *m_globals;
};
#endif
View
@@ -27,6 +27,34 @@ class Object {
Object() : m_id(-1) {}
+ enum VtableIdx {
+ idx__Hash__ = 0,
+ idx__Repr__ = 1,
+ idx__Str__ = 2,
+ idx__Cmp__ = 3,
+ idx__Getattr__ = 4,
+ idx__Setattr__ = 5,
+ idx__Hasattr__ = 6,
+ idx__StoreSubscr__ = 7,
+ idx__Subscr__ = 8,
+ idx__DelSubscr__ = 9
+ };
+
+ /*
+ * Virtual functions to be implemented by subclasses.
+ */
+ virtual Object *__Hash__();
+ virtual Object *__Repr__();
+ virtual Object *__Str__();
+ virtual Object *__Cmp__(Object *other);
+ virtual Object *__Getattr__(Object *attr);
+ virtual Object *__Setattr__(Object *attr, Object *value);
+ virtual Object *__Hasattr__(Object *attr);
+ virtual Object *__StoreSubscr__(Object *idx, Object *value);
+ virtual Object *__Subscr__(Object *idx);
+ virtual Object *__DelSubscr__(Object *idx);
+
+
/** Provide a string representation of the object. */
virtual const std::string Repr() = 0;
@@ -104,7 +132,7 @@ class Object {
}
protected:
- /** Return the 'wart' for this object type ('%', '@', '$') */
+ /** Return the 'wart' for this object type ('%', '@', '$', ':') */
virtual char GetWart() = 0;
virtual bool IsValue() {
return false;
View
@@ -1,20 +1,24 @@
#ifndef PY_RUNTIME_H
#define PY_RUNTIME_H
-#include <PyObject.h>
+#include <Object.h>
-struct FPyDict;
+class Dict;
+
+#define TRACE_C_CALLS 1
extern "C" {
- void *FPyRuntime_CheckCall(FPyObject *obj);
+ void *FPyRuntime_CheckCall(Object *obj);
+
+ Object *FPyRuntime_Print(Object *obj);
+ Object *FPyRuntime_PrintItem(Object *obj);
+ Object *FPyRuntime_PrintNewline();
- FPyObject *FPyRuntime_Print(FPyObject *obj);
- FPyObject *FPyRuntime_PrintItem(FPyObject *obj);
- FPyObject *FPyRuntime_PrintNewline();
+ Object *FPyRuntime_CallC_LJ(void *fn, Object *self, Object *p1, Object *p2, Object *p3, Object *p4, Object *p5);
}
-void PopulateDictWithBuiltins(FPyDict *dict);
+void PopulateDictWithBuiltins(Dict *dict);
#endif
View
@@ -0,0 +1,20 @@
+#ifndef TRACEBACK_H
+#define TRACEBACK_H
+
+#include <Constant.h>
+#include <list>
+#include <jit/jit.h>
+
+#define TRACEBACK_SHOW_BUILTINS 1
+
+class Traceback : public Constant {
+public:
+ const std::string Repr();
+
+ void AddTraceFromHere();
+
+private:
+ jit_stack_trace_t m_stack_trace;
+};
+
+#endif
View
@@ -12,7 +12,7 @@ class Function;
class Value : public Object {
public:
Value() :
- m_ty(0), m_jit_value(0) {
+ m_ty(0), m_jit_value(0), m_bytecode_offset(0) {
}
void SetType(Type *ty) {
@@ -35,6 +35,10 @@ class Value : public Object {
return m_jit_value;
}
+ void SetBytecodeOffset(int bo) {
+ m_bytecode_offset = bo;
+ }
+
private:
Type *m_ty;
@@ -43,6 +47,9 @@ class Value : public Object {
return NULL;
}
jit_value_t m_jit_value;
+
+protected:
+ int m_bytecode_offset;
};
#endif
View
@@ -11,12 +11,12 @@ class Function;
class ConstantInt : public Constant {
public:
- ConstantInt(int n) : m_n(n) {
+ ConstantInt(long n) : m_n(n) {
SetType(Type::GetIntTy());
}
virtual const std::string Repr();
private:
- int m_n;
+ long m_n;
};
class ConstantInt64 : public Constant {
public:
@@ -122,6 +122,8 @@ class Code : public Constant {
void Walk(Function *f);
+ int GetLineNo(int offs);
+
long m_argcount;
long m_kwonlyargcount;
long m_nlocals;
Oops, something went wrong.

0 comments on commit 069ec8f

Please sign in to comment.