From 5ca391c314ccef39597a5c412a27772f86e11889 Mon Sep 17 00:00:00 2001
From: David Robillard <d@drobilla.net>
Date: Sat, 14 May 2011 07:07:49 +0000
Subject: Make currentFn private to the backend.

git-svn-id: http://svn.drobilla.net/resp/trunk@421 ad02d1e2-f140-0410-9f75-f8b11f17cedd
---
 src/compile.cpp | 34 ++++++++++------------------------
 src/llvm.cpp    | 12 ++++++++++--
 src/repl.cpp    |  4 ++--
 src/resp.hpp    |  8 +++-----
 4 files changed, 25 insertions(+), 33 deletions(-)

(limited to 'src')

diff --git a/src/compile.cpp b/src/compile.cpp
index 1f82a47..1534263 100644
--- a/src/compile.cpp
+++ b/src/compile.cpp
@@ -52,14 +52,6 @@ compile_literal_symbol(CEnv& cenv, const ASymbol* sym) throw()
 	}
 }
 
-static CVal
-compile_cast(CEnv& cenv, const ATuple* cast) throw()
-{
-	return cenv.engine()->compileCast(cenv,
-	                                  resp_compile(cenv, cast->frst()),
-	                                  cenv.type(cast));
-}
-
 static CVal
 compile_type(CEnv& cenv, const AST* type) throw()
 {
@@ -137,22 +129,18 @@ static CVal
 compile_call(CEnv& cenv, const ATuple* call) throw()
 {
 	const ATuple* protT = cenv.type(call->fst())->as_tuple()->prot();
-	CFunc f = resp_compile(cenv, call->fst());
-
-	if (!f)
-		f = cenv.currentFn; // Recursive call (callee defined as a stub)
+	CFunc         f     = resp_compile(cenv, call->fst());
 
 	vector<CVal> args;
 	ATuple::const_iterator t = protT->iter_at(0);
 	for (ATuple::const_iterator e = call->iter_at(1); e != call->end(); ++e, ++t) {
 		CVal arg = resp_compile(cenv, *e);
-		if ((*e)->to_symbol()) {
-			if (cenv.type(*e) != cenv.type(*t)) {
-				args.push_back(cenv.engine()->compileCast(cenv, arg, *t));
-				continue;
-			}
+		if ((*e)->to_symbol() && (cenv.type(*e) != cenv.type(*t))) {
+
+			args.push_back(cenv.engine()->compileCast(cenv, arg, *t));
+		} else {
+			args.push_back(arg);
 		}
-		args.push_back(arg);
 	}
 
 	return cenv.engine()->compileCall(cenv, f, cenv.type(call->fst())->as_tuple(), args);
@@ -169,7 +157,6 @@ compile_fn_start(CEnv& cenv, const ATuple* call) throw()
 	cenv.def(name, NULL, type, func);
 
 	cenv.engine()->pushFnArgs(cenv, args, type, func);
-	cenv.currentFn = func;
 
 	return NULL;
 }
@@ -178,11 +165,8 @@ static CVal
 compile_fn_end(CEnv& cenv, const ATuple* call) throw()
 {
 	const AST* retT = cenv.type(call->frst())->as_tuple()->frrst();
-	cenv.engine()->finishFn(cenv, cenv.currentFn,
-	                        resp_compile(cenv, call->frrst()),
-	                        retT);
+	cenv.engine()->finishFn(cenv, resp_compile(cenv, call->frrst()), retT);
 	cenv.pop();
-	cenv.currentFn = NULL;
 	return NULL;
 }
 
@@ -214,7 +198,9 @@ resp_compile(CEnv& cenv, const AST* ast) throw()
 		if (is_primitive(cenv.penv, call))
 			return cenv.engine()->compilePrimitive(cenv, ast->as_tuple());
 		else if (form == "cast")
-			return compile_cast(cenv, call);
+			return cenv.engine()->compileCast(cenv,
+			                                  resp_compile(cenv, call->frst()),
+			                                  cenv.type(call));
 		else if (form == "cons" || isupper(form[0]))
 			return compile_cons(cenv, call);
 		else if (form == ".")
diff --git a/src/llvm.cpp b/src/llvm.cpp
index a0b635b..4bb21ec 100644
--- a/src/llvm.cpp
+++ b/src/llvm.cpp
@@ -79,7 +79,7 @@ struct LLVMEngine : public Engine {
 
 	CFunc startFn(CEnv& cenv, const string& name, const ATuple* args, const ATuple* type);
 	void  pushFnArgs(CEnv& cenv, const ATuple* prot, const ATuple* type, CFunc f);
-	void  finishFn(CEnv& cenv, CFunc f, CVal ret, const AST* retT);
+	void  finishFn(CEnv& cenv, CVal ret, const AST* retT);
 	void  eraseFn(CEnv& cenv, CFunc f);
 
 	CVal  compileCall(CEnv& cenv, CFunc f, const ATuple* funcT, const vector<CVal>& args);
@@ -129,12 +129,15 @@ private:
 	typedef std::stack<IfRecord*> IfStack;
 	IfStack if_stack;
 
+	CFunc currentFn;
+
 	unsigned labelIndex;
 };
 
 LLVMEngine::LLVMEngine()
 	: builder(context)
 	, opaqueT(NULL)
+	, currentFn(NULL)
 	, labelIndex(1)
 {
 	InitializeNativeTarget();
@@ -396,6 +399,7 @@ LLVMEngine::startFn(
 	BasicBlock* bb = BasicBlock::Create(context, "entry", f);
 	builder.SetInsertPoint(bb);
 
+	currentFn = f;
 	return f;
 }
 
@@ -420,8 +424,10 @@ LLVMEngine::pushFnArgs(CEnv& cenv, const ATuple* prot, const ATuple* type, CFunc
 }
 
 void
-LLVMEngine::finishFn(CEnv& cenv, CFunc f, CVal ret, const AST* retT)
+LLVMEngine::finishFn(CEnv& cenv, CVal ret, const AST* retT)
 {
+	CFunc f = currentFn;
+
 	if (retT->str() == "Nothing")
 		builder.CreateRetVoid();
 	else
@@ -433,6 +439,8 @@ LLVMEngine::finishFn(CEnv& cenv, CFunc f, CVal ret, const AST* retT)
 	}
 	if (cenv.args.find("-g") == cenv.args.end())
 		fnOpt->run(*static_cast<Function*>(f));
+
+	currentFn = NULL;
 }
 
 void
diff --git a/src/repl.cpp b/src/repl.cpp
index 216bbb2..83324f2 100644
--- a/src/repl.cpp
+++ b/src/repl.cpp
@@ -151,7 +151,7 @@ eval(CEnv& cenv, Cursor& cursor, istream& is, bool execute)
 			CVal  val = NULL;
 			for (Code::const_iterator i = exprs.begin(); i != exprs.end(); ++i)
 				val = resp_compile(cenv, *i);
-			cenv.engine()->finishFn(cenv, f, val, type);
+			cenv.engine()->finishFn(cenv, val, type);
 
 			// Call main and print result
 			if (cenv.args.find("-S") == cenv.args.end())
@@ -196,7 +196,7 @@ repl(CEnv& cenv)
 			try {
 				// Create function for this repl loop
 				f = cenv.engine()->startFn(cenv, replFnName, new ATuple(cursor), fnT);
-				cenv.engine()->finishFn(cenv, f, resp_compile(cenv, ast), type);
+				cenv.engine()->finishFn(cenv, resp_compile(cenv, ast), type);
 				callPrintCollect(cenv, f, ast, type, true);
 				if (cenv.args.find("-d") != cenv.args.end())
 					cenv.engine()->writeModule(cenv, cenv.out);
diff --git a/src/resp.hpp b/src/resp.hpp
index 5ee5047..94ea43a 100644
--- a/src/resp.hpp
+++ b/src/resp.hpp
@@ -673,8 +673,8 @@ struct Engine {
 	                         const ATuple* type,
 	                         CFunc         f) = 0;
 
-	virtual void  finishFn(CEnv& cenv, CFunc f, CVal ret, const AST* retT) = 0;
-	virtual void  eraseFn(CEnv& cenv, CFunc f)                             = 0;
+	virtual void  finishFn(CEnv& cenv, CVal ret, const AST* retT) = 0;
+	virtual void  eraseFn(CEnv& cenv, CFunc f)                    = 0;
 
 	virtual CVal  compileCall(CEnv& cenv, CFunc f, const ATuple* fT, CVals& args)           = 0;
 	virtual CVal  compileCast(CEnv& cenv, CVal v, const AST* t)                             = 0;
@@ -702,7 +702,7 @@ Engine* resp_new_c_engine();
 /// Compile-Time Environment
 struct CEnv {
 	CEnv(PEnv& p, TEnv& t, Engine* e, ostream& os=std::cout, ostream& es=std::cerr)
-		: out(os), err(es), penv(p), tenv(t), currentFn(NULL), repl(false), _engine(e)
+		: out(os), err(es), penv(p), tenv(t), repl(false), _engine(e)
 	{}
 
 	~CEnv() { Object::pool.collect(GC::Roots()); }
@@ -783,8 +783,6 @@ struct CEnv {
 	typedef map<const char*, CVal> CSyms;
 	CSyms cSyms;
 
-	CFunc currentFn; ///< Currently compiling function
-
 	bool repl;
 
 	struct FreeVars : public std::vector<const ASymbol*> {
-- 
cgit v1.2.1