Skip to content

Commit

Permalink
Initial attempt at allowing multiple OSLCompiler & OSLReader instances.
Browse files Browse the repository at this point in the history
  • Loading branch information
marsupial committed Jan 16, 2019
1 parent a32d056 commit e031f80
Show file tree
Hide file tree
Showing 19 changed files with 745 additions and 433 deletions.
8 changes: 1 addition & 7 deletions src/include/osl_pvt.h
Original file line number Diff line number Diff line change
Expand Up @@ -169,9 +169,7 @@ class TypeSpec {

/// Find a structure record by id number.
///
static StructSpec *structspec (int id) {
return id ? struct_list()[id].get() : NULL;
}
static StructSpec *structspec (int id);

/// Find a structure index by name, or return 0 if not found.
/// If 'add' is true, add the struct if not already found.
Expand All @@ -181,10 +179,6 @@ class TypeSpec {
///
static int new_struct (StructSpec *n);

/// Return a reference to the structure list.
///
static std::vector<std::shared_ptr<StructSpec> > & struct_list ();

/// Is this an array (either a simple array, or an array of structs)?
///
bool is_array () const { return m_simple.arraylen != 0; }
Expand Down
22 changes: 11 additions & 11 deletions src/liboslcomp/ast.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ ASTfunction_declaration::ASTfunction_declaration (OSLCompilerImpl *comp,
error ("\"%s\" : sorry, can't start with three underscores", name);

// Get a pointer to the first of the existing symbols of that name.
Symbol *existing_syms = comp->symtab().clash (name);
Symbol *existing_syms = m_compiler->symtab().clash (name);
if (existing_syms && existing_syms->symtype() != SymTypeFunction) {
error ("\"%s\" already declared in this scope as a %s",
name, existing_syms->typespec());
Expand All @@ -385,14 +385,14 @@ ASTfunction_declaration::ASTfunction_declaration (OSLCompilerImpl *comp,

// Build up the argument signature for this declared function
m_typespec = type;
std::string argcodes = oslcompiler->code_from_type (m_typespec);
std::string argcodes = m_compiler->code_from_type (m_typespec);
for (ASTNode *arg = form; arg; arg = arg->nextptr()) {
const TypeSpec &t (arg->typespec());
if (t == TypeSpec() /* UNKNOWN */) {
m_typespec = TypeDesc::UNKNOWN;
return;
}
argcodes += oslcompiler->code_from_type (t);
argcodes += m_compiler->code_from_type (t);
ASSERT (arg->nodetype() == variable_declaration_node);
ASTvariable_declaration *v = (ASTvariable_declaration *)arg;
if (v->init())
Expand All @@ -404,7 +404,7 @@ ASTfunction_declaration::ASTfunction_declaration (OSLCompilerImpl *comp,
// same polymorphic type in the same scope.
if (stmts) {
std::string err;
int current_scope = oslcompiler->symtab().scopeid();
int current_scope = m_compiler->symtab().scopeid();
for (FunctionSymbol *f = static_cast<FunctionSymbol *>(existing_syms);
f; f = f->nextpoly()) {
if (f->scope() == current_scope && f->argcodes() == argcodes) {
Expand Down Expand Up @@ -438,7 +438,7 @@ ASTfunction_declaration::ASTfunction_declaration (OSLCompilerImpl *comp,
func()->nextpoly ((FunctionSymbol *)existing_syms);

func()->argcodes (ustring (argcodes));
oslcompiler->symtab().insert (m_sym);
m_compiler->symtab().insert (m_sym);

// Typecheck it right now, upon declaration
typecheck (typespec ());
Expand Down Expand Up @@ -528,7 +528,7 @@ ASTvariable_declaration::ASTvariable_declaration (OSLCompilerImpl *comp,
}

m_typespec = type;
Symbol *f = comp->symtab().clash (name);
Symbol *f = m_compiler->symtab().clash (name);
if (f && ! m_ismetadata) {
std::string e = Strutil::sprintf ("\"%s\" already declared in this scope", name.c_str());
if (f->node()) {
Expand All @@ -554,7 +554,7 @@ ASTvariable_declaration::ASTvariable_declaration (OSLCompilerImpl *comp,
symtype = SymTypeTemp;
m_sym = new Symbol (name, type, symtype, this);
if (! m_ismetadata)
oslcompiler->symtab().insert (m_sym);
m_compiler->symtab().insert (m_sym);

// A struct really makes several subvariables
if (type.is_structure() || type.is_structure_array()) {
Expand Down Expand Up @@ -608,7 +608,7 @@ ASTvariable_declaration::print (std::ostream &out, int indentlevel) const
ASTvariable_ref::ASTvariable_ref (OSLCompilerImpl *comp, ustring name)
: ASTNode (variable_ref_node, comp), m_name(name), m_sym(NULL)
{
m_sym = comp->symtab().find (name);
m_sym = m_compiler->symtab().find (name);
if (! m_sym) {
error ("'%s' was not declared in this scope", name.c_str());
// FIXME -- would be fun to troll through the symtab and try to
Expand Down Expand Up @@ -1040,7 +1040,7 @@ ASTunary_expression::ASTunary_expression (OSLCompilerImpl *comp, int op,
: ASTNode (unary_expression_node, comp, op, expr)
{
// Check for a user-overloaded function for this operator
Symbol *sym = comp->symtab().find (ustring::format ("__operator__%s__", opword()));
Symbol *sym = m_compiler->symtab().find (ustring::format ("__operator__%s__", opword()));
if (sym && sym->symtype() == SymTypeFunction)
m_function_overload = (FunctionSymbol *)sym;
}
Expand Down Expand Up @@ -1092,7 +1092,7 @@ ASTbinary_expression::ASTbinary_expression (OSLCompilerImpl *comp, Operator op,
// Disallow a few ops from overloading.
if (op != And && op != Or) {
ustring funcname = ustring::format ("__operator__%s__", opword());
Symbol *sym = comp->symtab().find (funcname);
Symbol *sym = m_compiler->symtab().find (funcname);
if (sym && sym->symtype() == SymTypeFunction)
m_function_overload = (FunctionSymbol *)sym;
}
Expand Down Expand Up @@ -1196,7 +1196,7 @@ ASTtype_constructor::childname (size_t i) const
ASTfunction_call::ASTfunction_call (OSLCompilerImpl *comp, ustring name,
ASTNode *args, FunctionSymbol *funcsym)
: ASTNode (function_call_node, comp, 0, args), m_name(name),
m_sym(funcsym ? funcsym : comp->symtab().find (name)), // Look it up.
m_sym(funcsym ? funcsym : m_compiler->symtab().find (name)), // Look it up.
m_poly(funcsym), // Default - resolved symbol or null
m_argread(~1), // Default - all args are read except the first
m_argwrite(1), // Default - first arg only is written by the op
Expand Down
1 change: 0 additions & 1 deletion src/liboslcomp/ast.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.


class oslFlexLexer;
extern int oslparse ();


OSL_NAMESPACE_ENTER
Expand Down
38 changes: 19 additions & 19 deletions src/liboslcomp/codegen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ OSLCompilerImpl::add_struct_fields (StructSpec *structspec,
}
Symbol *sym = new Symbol (fieldname, type, symtype, node);
sym->fieldid (i);
oslcompiler->symtab().insert (sym);
symtab().insert (sym);
if (field.type.is_structure() || field.type.is_structure_array()) {
// nested structures -- recurse!
add_struct_fields (type.structspec(), fieldname, symtype, arr, node,
Expand Down Expand Up @@ -422,7 +422,7 @@ ASTshader_declaration::codegen (Symbol *)
Symbol *
ASTreturn_statement::codegen (Symbol *dest)
{
FunctionSymbol *myfunc = oslcompiler->current_function ();
FunctionSymbol *myfunc = m_compiler->current_function ();
if (myfunc) {
// If it's a user function (as opposed to a main shader body)...
if (expr()) {
Expand Down Expand Up @@ -1377,17 +1377,17 @@ ASTconditional_statement::codegen (Symbol *)
// can go back and patch it with the jump destinations.
int ifop = emitcode ("if", condvar);
// "if" is unusual in that it doesn't write its first argument
oslcompiler->lastop().argread (0, true);
oslcompiler->lastop().argwrite (0, false);
m_compiler->lastop().argread (0, true);
m_compiler->lastop().argwrite (0, false);

// Generate the code for the 'true' and 'false' code blocks, recording
// the jump destinations for 'else' and the next op after the if.
oslcompiler->push_nesting (false);
m_compiler->push_nesting (false);
codegen_list (truestmt());
int falselabel = m_compiler->next_op_label ();
codegen_list (falsestmt());
int donelabel = m_compiler->next_op_label ();
oslcompiler->pop_nesting (false);
m_compiler->pop_nesting (false);

// Fix up the 'if' to have the jump destinations.
m_compiler->ircode(ifop).set_jump (falselabel, donelabel);
Expand All @@ -1407,10 +1407,10 @@ ASTloop_statement::codegen (Symbol *)
// can go back and patch it with the jump destinations.
int loop_op = emitcode (opname());
// Loop ops read their first arg, not write it
oslcompiler->lastop().argread (0, true);
oslcompiler->lastop().argwrite (0, false);
m_compiler->lastop().argread (0, true);
m_compiler->lastop().argwrite (0, false);

oslcompiler->push_nesting (true);
m_compiler->push_nesting (true);
codegen_list (init());

int condlabel = m_compiler->next_op_label ();
Expand All @@ -1427,7 +1427,7 @@ ASTloop_statement::codegen (Symbol *)
int iterlabel = m_compiler->next_op_label ();
codegen_list (iter());
int donelabel = m_compiler->next_op_label ();
oslcompiler->pop_nesting (true);
m_compiler->pop_nesting (true);

// Fix up the loop op to have the jump destinations.
m_compiler->ircode(loop_op).set_jump (condlabel, bodylabel,
Expand Down Expand Up @@ -1577,8 +1577,8 @@ ASTbinary_expression::codegen_logic (Symbol *dest)

int ifop = emitcode ("if", dest);
// "if" is unusual in that it doesn't write its first argument
oslcompiler->lastop().argread (0, true);
oslcompiler->lastop().argwrite (0, false);
m_compiler->lastop().argread (0, true);
m_compiler->lastop().argwrite (0, false);
int falselabel;
m_compiler->push_nesting (false);

Expand Down Expand Up @@ -1614,25 +1614,25 @@ ASTternary_expression::codegen (Symbol *dest)
// can go back and patch it with the jump destinations.
int ifop = emitcode ("if", condvar);
// "if" is unusual in that it doesn't write its first argument
oslcompiler->lastop().argread (0, true);
oslcompiler->lastop().argwrite (0, false);
m_compiler->lastop().argread (0, true);
m_compiler->lastop().argwrite (0, false);

// Generate the code for the 'true' and 'false' code blocks, recording
// the jump destinations for 'else' and the next op after the if.
oslcompiler->push_nesting (false);
m_compiler->push_nesting (false);
Symbol *trueval = trueexpr()->codegen (dest);
if (trueval != dest)
emitcode ("assign", dest, trueval);

int falselabel = m_compiler->next_op_label ();

oslcompiler->push_nesting (false);
m_compiler->push_nesting (false);
Symbol *falseval = falseexpr()->codegen (dest);
if (falseval != dest)
emitcode ("assign", dest, falseval);

int donelabel = m_compiler->next_op_label ();
oslcompiler->pop_nesting (false);
m_compiler->pop_nesting (false);

// Fix up the 'if' to have the jump destinations.
m_compiler->ircode(ifop).set_jump (falselabel, donelabel);
Expand Down Expand Up @@ -1885,9 +1885,9 @@ ASTfunction_call::codegen (Symbol *dest)
m_compiler->make_constant(m_name));

// Generate the code for the function body
oslcompiler->push_function (func ());
m_compiler->push_function (func ());
codegen_list (user_function()->statements());
oslcompiler->pop_function ();
m_compiler->pop_function ();

// Go back and mark the "functioncall" with the right jump address
m_compiler->ircode(loop_op).argread (0, true); // read
Expand Down
11 changes: 0 additions & 11 deletions src/liboslcomp/oslcomp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,6 @@ OSLCompiler::output_filename () const
namespace pvt { // OSL::pvt


OSLCompilerImpl *oslcompiler = nullptr;
static std::mutex oslcompiler_mutex;

static ustring op_for("for");
static ustring op_while("while");
static ustring op_dowhile("dowhile");
Expand Down Expand Up @@ -559,9 +556,6 @@ OSLCompilerImpl::compile (string_view filename,
} else if (m_preprocess_only) {
std::cout << preprocess_result;
} else {
// Thread safety with the lexer/parser
std::lock_guard<std::mutex> lock (oslcompiler_mutex);
oslcompiler = this;
bool parseerr = osl_parse_buffer (preprocess_result);
if (! parseerr) {
if (shader())
Expand Down Expand Up @@ -604,7 +598,6 @@ OSLCompilerImpl::compile (string_view filename,
ASSERT (m_osofile == NULL);
}

oslcompiler = nullptr;
}

return ! error_encountered();
Expand Down Expand Up @@ -645,9 +638,6 @@ OSLCompilerImpl::compile_buffer (string_view sourcecode,
} else if (m_preprocess_only) {
std::cout << preprocess_result;
} else {
// Thread safety with the lexer/parser
std::lock_guard<std::mutex> lock (oslcompiler_mutex);
oslcompiler = this;
bool parseerr = osl_parse_buffer (preprocess_result);
if (! parseerr) {
if (shader())
Expand Down Expand Up @@ -686,7 +676,6 @@ OSLCompilerImpl::compile_buffer (string_view sourcecode,
ASSERT (m_osofile == NULL);
}

oslcompiler = nullptr;
}

return ! error_encountered();
Expand Down
3 changes: 0 additions & 3 deletions src/liboslcomp/oslcomp_pvt.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include <OSL/genclosure.h>


extern int oslparse ();


OSL_NAMESPACE_ENTER
Expand Down Expand Up @@ -504,8 +503,6 @@ class OSLCompilerImpl {
};


extern OSLCompilerImpl *oslcompiler;


}; // namespace pvt

Expand Down
Loading

0 comments on commit e031f80

Please sign in to comment.