Commit 6926ccff authored by Marius Wachtler's avatar Marius Wachtler

AST: deallocate nodes

Use a bumpptr allocator to keep track of the AST nodes.
I could not use the LLVM one because it does not call the destructor on allocated elements.
parent 0dea31b9
This diff is collapsed.
...@@ -25,7 +25,7 @@ namespace pyston { ...@@ -25,7 +25,7 @@ namespace pyston {
// Convert a CPython ast object to a Pyston ast object. // Convert a CPython ast object to a Pyston ast object.
// This will also check for certain kinds of "syntax errors" (ex continue not in loop) and will // This will also check for certain kinds of "syntax errors" (ex continue not in loop) and will
// throw them as C++ exceptions. // throw them as C++ exceptions.
AST* cpythonToPystonAST(mod_ty mod, llvm::StringRef fn); std::pair<AST*, std::unique_ptr<ASTAllocator>> cpythonToPystonAST(mod_ty mod, llvm::StringRef fn);
} }
#endif #endif
...@@ -292,7 +292,9 @@ extern "C" PyCodeObject* PyAST_Compile(struct _mod* _mod, const char* filename, ...@@ -292,7 +292,9 @@ extern "C" PyCodeObject* PyAST_Compile(struct _mod* _mod, const char* filename,
PyArena* arena) noexcept { PyArena* arena) noexcept {
try { try {
mod_ty mod = _mod; mod_ty mod = _mod;
AST* parsed = cpythonToPystonAST(mod, filename); std::unique_ptr<ASTAllocator> ast_allocator;
AST* parsed;
std::tie(parsed, ast_allocator) = cpythonToPystonAST(mod, filename);
BoxedCode* code = NULL; BoxedCode* code = NULL;
switch (mod->kind) { switch (mod->kind) {
case Module_kind: case Module_kind:
......
This diff is collapsed.
...@@ -20,10 +20,11 @@ ...@@ -20,10 +20,11 @@
namespace pyston { namespace pyston {
class AST_Module; class AST_Module;
class ASTAllocator;
AST_Module* parse_string(const char* code, FutureFlags inherited_flags); std::pair<AST_Module*, std::unique_ptr<ASTAllocator>> parse_string(const char* code, FutureFlags inherited_flags);
AST_Module* parse_file(const char* fn, FutureFlags inherited_flags); std::pair<AST_Module*, std::unique_ptr<ASTAllocator>> parse_file(const char* fn, FutureFlags inherited_flags);
AST_Module* caching_parse_file(const char* fn, FutureFlags inherited_flags); std::pair<AST_Module*, std::unique_ptr<ASTAllocator>> caching_parse_file(const char* fn, FutureFlags inherited_flags);
} }
#endif #endif
This diff is collapsed.
This diff is collapsed.
...@@ -460,7 +460,9 @@ static int main(int argc, char** argv) noexcept { ...@@ -460,7 +460,9 @@ static int main(int argc, char** argv) noexcept {
if (command != NULL) { if (command != NULL) {
try { try {
main_module = createModule(autoDecref(boxString("__main__")), "<string>"); main_module = createModule(autoDecref(boxString("__main__")), "<string>");
AST_Module* m = parse_string(command, /* future_flags = */ 0); AST_Module* m;
std::unique_ptr<ASTAllocator> ast_allocator;
std::tie(m, ast_allocator) = parse_string(command, /* future_flags = */ 0);
compileAndRunModule(m, main_module); compileAndRunModule(m, main_module);
rtncode = 0; rtncode = 0;
} catch (ExcInfo e) { } catch (ExcInfo e) {
...@@ -504,7 +506,10 @@ static int main(int argc, char** argv) noexcept { ...@@ -504,7 +506,10 @@ static int main(int argc, char** argv) noexcept {
free(real_path); free(real_path);
try { try {
AST_Module* ast = parse_file(fn, /* future_flags = */ 0); std::unique_ptr<ASTAllocator> ast_allocator;
AST_Module* ast;
std::tie(ast, ast_allocator) = parse_file(fn, /* future_flags = */ 0);
compileAndRunModule(ast, main_module); compileAndRunModule(ast, main_module);
rtncode = 0; rtncode = 0;
} catch (ExcInfo e) { } catch (ExcInfo e) {
......
...@@ -37,6 +37,7 @@ ...@@ -37,6 +37,7 @@
#include "capi/types.h" #include "capi/types.h"
#include "codegen/irgen/hooks.h" #include "codegen/irgen/hooks.h"
#include "codegen/unwinding.h" #include "codegen/unwinding.h"
#include "core/ast.h"
#include "core/threading.h" #include "core/threading.h"
#include "core/types.h" #include "core/types.h"
#include "runtime/classobj.h" #include "runtime/classobj.h"
...@@ -847,8 +848,8 @@ extern "C" int PyRun_InteractiveOneFlags(FILE* fp, const char* filename, PyCompi ...@@ -847,8 +848,8 @@ extern "C" int PyRun_InteractiveOneFlags(FILE* fp, const char* filename, PyCompi
bool failed = false; bool failed = false;
try { try {
assert(mod->kind == Interactive_kind); assert(mod->kind == Interactive_kind);
AST_Module* pyston_module = static_cast<AST_Module*>(cpythonToPystonAST(mod, filename)); auto res = cpythonToPystonAST(mod, filename);
compileAndRunModule(pyston_module, static_cast<BoxedModule*>(m)); compileAndRunModule((AST_Module*)res.first, static_cast<BoxedModule*>(m));
} catch (ExcInfo e) { } catch (ExcInfo e) {
setCAPIException(e); setCAPIException(e);
failed = true; failed = true;
......
...@@ -25,6 +25,7 @@ ...@@ -25,6 +25,7 @@
#include "codegen/irgen/hooks.h" #include "codegen/irgen/hooks.h"
#include "codegen/parser.h" #include "codegen/parser.h"
#include "codegen/unwinding.h" #include "codegen/unwinding.h"
#include "core/ast.h"
#include "runtime/inline/list.h" #include "runtime/inline/list.h"
#include "runtime/objmodel.h" #include "runtime/objmodel.h"
#include "runtime/util.h" #include "runtime/util.h"
...@@ -87,7 +88,9 @@ extern "C" PyObject* load_source_module(char* name, char* pathname, FILE* fp) no ...@@ -87,7 +88,9 @@ extern "C" PyObject* load_source_module(char* name, char* pathname, FILE* fp) no
AUTO_DECREF(name_boxed); AUTO_DECREF(name_boxed);
try { try {
BoxedModule* module = createModule(name_boxed, pathname); BoxedModule* module = createModule(name_boxed, pathname);
AST_Module* ast = caching_parse_file(pathname, /* future_flags = */ 0); std::unique_ptr<ASTAllocator> ast_allocator;
AST_Module* ast;
std::tie(ast, ast_allocator) = caching_parse_file(pathname, /* future_flags = */ 0);
assert(ast); assert(ast);
compileAndRunModule(ast, module); compileAndRunModule(ast, module);
Box* r = getSysModulesDict()->getOrNull(name_boxed); Box* r = getSysModulesDict()->getOrNull(name_boxed);
...@@ -118,7 +121,9 @@ extern "C" PyObject* PyImport_ExecCodeModuleEx(const char* name, PyObject* co, c ...@@ -118,7 +121,9 @@ extern "C" PyObject* PyImport_ExecCodeModuleEx(const char* name, PyObject* co, c
static BoxedString* file_str = getStaticString("__file__"); static BoxedString* file_str = getStaticString("__file__");
module->setattr(file_str, autoDecref(boxString(pathname)), NULL); module->setattr(file_str, autoDecref(boxString(pathname)), NULL);
AST_Module* ast = parse_string(code->data(), /* future_flags = */ 0); AST_Module* ast;
std::unique_ptr<ASTAllocator> ast_allocator;
std::tie(ast, ast_allocator) = parse_string(code->data(), /* future_flags = */ 0);
compileAndRunModule(ast, module); compileAndRunModule(ast, module);
return incref(module); return incref(module);
} catch (ExcInfo e) { } catch (ExcInfo e) {
......
...@@ -29,7 +29,9 @@ protected: ...@@ -29,7 +29,9 @@ protected:
#ifndef NDEBUG #ifndef NDEBUG
TEST_F(AnalysisTest, augassign) { TEST_F(AnalysisTest, augassign) {
const std::string fn("test/unittests/analysis_listcomp.py"); const std::string fn("test/unittests/analysis_listcomp.py");
AST_Module* module = caching_parse_file(fn.c_str(), 0); std::unique_ptr<ASTAllocator> ast_allocator;
AST_Module* module;
std::tie(module, ast_allocator) = caching_parse_file(fn.c_str(), 0);
assert(module); assert(module);
FutureFlags future_flags = getFutureFlags(module->body, fn.c_str()); FutureFlags future_flags = getFutureFlags(module->body, fn.c_str());
...@@ -45,7 +47,7 @@ TEST_F(AnalysisTest, augassign) { ...@@ -45,7 +47,7 @@ TEST_F(AnalysisTest, augassign) {
ASSERT_NE(scope_info->getScopeTypeOfName(module->interned_strings->get("a")), ScopeInfo::VarScopeType::GLOBAL); ASSERT_NE(scope_info->getScopeTypeOfName(module->interned_strings->get("a")), ScopeInfo::VarScopeType::GLOBAL);
ASSERT_FALSE(scope_info->getScopeTypeOfName(module->interned_strings->get("b")) == ScopeInfo::VarScopeType::GLOBAL); ASSERT_FALSE(scope_info->getScopeTypeOfName(module->interned_strings->get("b")) == ScopeInfo::VarScopeType::GLOBAL);
AST_arguments* args = new AST_arguments(); AST_arguments* args = new (*ast_allocator) AST_arguments();
ParamNames param_names(args, *module->interned_strings.get()); ParamNames param_names(args, *module->interned_strings.get());
// Hack to get at the cfg: // Hack to get at the cfg:
...@@ -68,7 +70,9 @@ TEST_F(AnalysisTest, augassign) { ...@@ -68,7 +70,9 @@ TEST_F(AnalysisTest, augassign) {
void doOsrTest(bool is_osr, bool i_maybe_undefined) { void doOsrTest(bool is_osr, bool i_maybe_undefined) {
const std::string fn("test/unittests/analysis_osr.py"); const std::string fn("test/unittests/analysis_osr.py");
AST_Module* module = caching_parse_file(fn.c_str(), 0); std::unique_ptr<ASTAllocator> ast_allocator;
AST_Module* module;
std::tie(module, ast_allocator) = caching_parse_file(fn.c_str(), 0);
assert(module); assert(module);
ParamNames param_names(NULL, *module->interned_strings.get()); ParamNames param_names(NULL, *module->interned_strings.get());
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment