Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added exception handling for libjit

  • Loading branch information...
commit 6b5055d52e1321c2c7c6ad6f2e3caa652f59f0f0 1 parent 2fa55e9
@jmolloy authored
View
6 include/BasicBlock.h
@@ -89,9 +89,13 @@ class BasicBlock : public Value {
void SetUnwindBlock(BasicBlock *uw) {
m_unwind_block = uw;
}
+ BasicBlock *GetUnwindBlock() {
+ return m_unwind_block;
+ }
void LJ_Codegen();
jit_label_t *LJ_GetLabel();
+ jit_label_t *LJ_GetEndLabel();
protected:
char GetWart() {
@@ -125,7 +129,7 @@ class BasicBlock : public Value {
BasicBlock *m_unwind_block;
- jit_label_t m_jit_label;
+ jit_label_t m_jit_label, m_jit_end_label;
};
#endif
View
2  include/PyRuntime.h
@@ -10,6 +10,8 @@ extern "C" {
void *PyRuntime_CheckCall(PyObject *obj);
PyObject *PyRuntime_Print(PyObject *obj);
+ PyObject *PyRuntime_PrintItem(PyObject *obj);
+ PyObject *PyRuntime_PrintNewline();
}
View
4 include/instructions.h
@@ -148,14 +148,14 @@ class PrintItem : public Instruction {
Instruction("print-item", id) {
m_args.push_back(v);
}
-
+ virtual jit_value_t _LJ_Codegen(jit_function_t func, Function *f);
};
class PrintNewline : public Instruction {
public:
PrintNewline(int id) :
Instruction("print-newline", id) {
}
-
+ virtual jit_value_t _LJ_Codegen(jit_function_t func, Function *f);
};
class ReRaise : public Instruction {
public:
View
7 src/BasicBlock.cc
@@ -18,6 +18,7 @@
BasicBlock::BasicBlock(Function *parent) :
m_num_successors(0), m_stack(new OperandStack()), m_fn(parent),
m_jit_label(jit_label_undefined),
+ m_jit_end_label(jit_label_undefined),
m_unwind_block(NULL)
{
m_successors[0] = NULL;
@@ -329,8 +330,14 @@ void BasicBlock::LJ_Codegen() {
++it) {
(*it)->LJ_Codegen(func, m_fn);
}
+
+ jit_insn_label(func, &m_jit_end_label);
}
jit_label_t *BasicBlock::LJ_GetLabel() {
return &m_jit_label;
}
+
+jit_label_t *BasicBlock::LJ_GetEndLabel() {
+ return &m_jit_end_label;
+}
View
29 src/Function.cc
@@ -5,6 +5,8 @@
#include <sstream>
#include <Module.h>
+#include <stdlib.h>
+
std::map<std::string, Function*> g_functions;
Function::Function(std::string name, Code *code, Module *module) :
@@ -68,6 +70,17 @@ jit_function_t Function::LJ_Codegen(jit_context_t ctx) {
m_jit_function = jit_function_create(ctx, signature);
+ bool uses_catcher = false;
+ for(std::vector<BasicBlock*>::iterator it = m_blocks.begin();
+ it != m_blocks.end();
+ ++it) {
+ if((*it)->GetUnwindBlock()) {
+ jit_insn_uses_catcher(m_jit_function);
+ uses_catcher = true;
+ break;
+ }
+ }
+
GetEntryBlock()->LJ_Codegen();
for(std::vector<BasicBlock*>::iterator it = m_blocks.begin();
it != m_blocks.end();
@@ -75,6 +88,22 @@ jit_function_t Function::LJ_Codegen(jit_context_t ctx) {
(*it)->LJ_Codegen();
}
+ if(uses_catcher) {
+ jit_insn_start_catcher(m_jit_function);
+
+ for(std::vector<BasicBlock*>::iterator it = m_blocks.begin();
+ it != m_blocks.end();
+ ++it) {
+ if((*it)->GetUnwindBlock()) {
+ jit_insn_branch_if_pc_not_in_range(m_jit_function,
+ *(*it)->LJ_GetLabel(),
+ *(*it)->LJ_GetEndLabel(),
+ (*it)->GetUnwindBlock()->LJ_GetLabel());
+ }
+ jit_insn_rethrow_unhandled(m_jit_function);
+ }
+
+ }
jit_function_compile(m_jit_function);
return m_jit_function;
View
109 src/LJ_Codegen.cc
@@ -10,6 +10,40 @@
#include <stdio.h>
+static jit_type_t _LJ_fn_signature(int nargs) {
+ jit_type_t *tyargs = new jit_type_t[nargs];
+ for(int i = 0; i < nargs; i++) {
+ tyargs[i] = jit_type_nuint;
+ }
+ jit_type_t sig = jit_type_create_signature(jit_abi_cdecl,
+ jit_type_nuint,
+ tyargs,
+ nargs,
+ 1);
+ delete [] tyargs;
+ return sig;
+}
+
+static jit_value_t _LJ_Call(jit_function_t func, const char *name, void *fp) {
+ return jit_insn_call_native(func, name, fp, _LJ_fn_signature(0), 0, 0, 0);
+}
+static jit_value_t _LJ_Call(jit_function_t func, const char *name, void *fp, jit_value_t arg0) {
+ return jit_insn_call_native(func, name, fp, _LJ_fn_signature(1), &arg0, 1, 0);
+}
+static jit_value_t _LJ_Call(jit_function_t func, const char *name, void *fp, jit_value_t arg0, jit_value_t arg1) {
+ jit_value_t args[2];
+ args[0] = arg0;
+ args[1] = arg1;
+ return jit_insn_call_native(func, name, fp, _LJ_fn_signature(2), args, 2, 0);
+}
+static jit_value_t _LJ_Call(jit_function_t func, const char *name, void *fp, jit_value_t arg0, jit_value_t arg1, jit_value_t arg2) {
+ jit_value_t args[3];
+ args[0] = arg0;
+ args[1] = arg1;
+ args[2] = arg2;
+ return jit_insn_call_native(func, name, fp, _LJ_fn_signature(3), args, 3, 0);
+}
+
jit_value_t LoadGlobal::_LJ_Codegen(jit_function_t func, Function *f) {
jit_value_t g = jit_value_create_nint_constant(func, jit_type_nuint, (jit_nint)f->GetModule()->GetGlobals());
@@ -20,24 +54,7 @@ jit_value_t LoadGlobal::_LJ_Codegen(jit_function_t func, Function *f) {
hash_value = jit_value_create_nint_constant(func, jit_type_nuint, 2);
}
- jit_type_t tyargs[3];
- tyargs[0] = jit_type_nuint;
- tyargs[1] = jit_type_nuint;
- tyargs[2] = jit_type_nuint;
-
- jit_type_t sig = jit_type_create_signature(jit_abi_cdecl,
- jit_type_nuint,
- tyargs,
- 3,
- 1);
-
- jit_value_t args[3];
- args[0] = g;
- args[1] = hash_value;
- args[2] = m_args[0]->LJ_Codegen(func, f);
- jit_value_t call = jit_insn_call_native(func, "PyDict_lookup", (void*)&PyDict_Lookup, sig, args, 3, 0);
-
- return call;
+ return _LJ_Call(func, "PyDict_lookup", (void*)&PyDict_Lookup, g, hash_value, m_args[0]->LJ_Codegen(func, f));
}
jit_value_t Return::_LJ_Codegen(jit_function_t func, Function *f) {
@@ -52,19 +69,8 @@ jit_value_t Call::_LJ_Codegen(jit_function_t func, Function *f) {
/** @todo Keyword args */
int nargs = m_positional_args.size();
- jit_type_t *tyargs2 = new jit_type_t[nargs];
- for(int i = 0; i < nargs; i++) {
- tyargs2[i] = jit_type_nuint;
- }
-
- jit_type_t sig_call = jit_type_create_signature(jit_abi_cdecl,
- jit_type_nuint,
- tyargs2,
- nargs,
- 1);
-
- delete [] tyargs2;
+ jit_type_t sig_call = _LJ_fn_signature(nargs);
jit_type_t sig = jit_type_create_signature(jit_abi_cdecl,
sig_call,
@@ -89,45 +95,10 @@ jit_value_t Call::_LJ_Codegen(jit_function_t func, Function *f) {
}
jit_value_t PrintItem::_LJ_Codegen(jit_function_t func, Function *f) {
- /* Call out to an external helper that will provide type checking */
- jit_type_t tyargs[1];
- tyargs[0] = jit_type_nuint;
-
- int nargs = m_positional_args.size();
- jit_type_t *tyargs2 = new jit_type_t[nargs];
- for(int i = 0; i < nargs; i++) {
- tyargs2[i] = jit_type_nuint;
- }
-
- jit_type_t sig_call = jit_type_create_signature(jit_abi_cdecl,
- jit_type_nuint,
- tyargs2,
- nargs,
- 1);
-
- delete [] tyargs2;
-
-
- jit_type_t sig = jit_type_create_signature(jit_abi_cdecl,
- sig_call,
- tyargs,
- 1,
- 1);
-
- jit_value_t arg = m_callee->LJ_Codegen(func, f);
- jit_value_t callee = jit_insn_call_native(func, "PyRuntime_Print", (void*)&PyRuntime_CheckCall, sig, &arg, 1, 0);
-
- /* Then call the function itself */
-
- jit_value_t *args = new jit_value_t[nargs];
- int i = 0;
- for(std::list<Value*>::iterator it = m_positional_args.begin();
- i < nargs;
- it++) {
- args[i++] = (*it)->LJ_Codegen(func, f);
- }
-
- return jit_insn_call_indirect(func, callee, sig_call, args, nargs, 0);
+ return _LJ_Call(func, "PyRuntime_PrintItem", (void*)&PyRuntime_PrintItem, m_args[0]->LJ_Codegen(func, f));
+}
+jit_value_t PrintNewline::_LJ_Codegen(jit_function_t func, Function *f) {
+ return _LJ_Call(func, "PyRuntime_PrintNewline", (void*)&PyRuntime_PrintNewline);
}
View
16 src/PyRuntime.cc
@@ -8,6 +8,8 @@
#include <stdlib.h>
#include <stdio.h>
+#include <jit/jit.h>
+
void *PyRuntime_CheckCall(PyObject *obj) {
if(obj == NULL) {
/**@todo Throw exception */
@@ -32,6 +34,20 @@ PyObject *PyRuntime_Print(PyObject *obj) {
return PyNone_Create();
}
+PyObject *PyRuntime_PrintItem(PyObject *obj) {
+ if(obj->tag == Type::TagFor(Type::GetStringTy())) {
+ printf("%s", ((PyString*)obj)->str );
+ } else {
+ /** @todo Throw exception */
+ abort();
+ }
+
+ return PyNone_Create();
+}
+PyObject *PyRuntime_PrintNewline() {
+ printf("\n");
+ return PyNone_Create();
+}
void PopulateDictWithBuiltins(PyDict *dict) {
PyDict_Insert(dict, PyString_Create("print"), PyFunction_Create((void*)&PyRuntime_Print));
View
2  src/main.cc
@@ -51,7 +51,7 @@ int main(int argc, char **argv) {
jit_context_build_start(ctx);
__main__->LJ_Codegen(ctx);
jit_context_build_end(ctx);
-
+// exit(0);
unsigned long result;
jit_function_apply(__main__->GetMainFunction()->LJ_Codegen(ctx),
0,
Please sign in to comment.
Something went wrong with that request. Please try again.