remove stray tab
[mesa.git] / src / mesa / shader / slang / slang_compile.c
index 463088fc5cdc52bbf9b45f1fb920f2675f25c39c..c459eb29e744a72baea680004c666cc59bf3a7f0 100644 (file)
@@ -38,6 +38,8 @@
 #include "slang_preprocess.h"
 #include "slang_storage.h"
 #include "slang_error.h"
+#include "slang_emit.h"
+#include "slang_vartable.h"
 
 #include "slang_print.h"
 
@@ -97,7 +99,9 @@ _slang_code_object_ctr(slang_code_object * self)
    for (i = 0; i < SLANG_BUILTIN_TOTAL; i++)
       _slang_code_unit_ctr(&self->builtin[i], self);
    _slang_code_unit_ctr(&self->unit, self);
+#if 01
    _slang_assembly_file_ctr(&self->assembly);
+#endif
    slang_machine_ctr(&self->machine);
    self->varpool.next_addr = 0;
    slang_atom_pool_construct(&self->atompool);
@@ -115,7 +119,9 @@ _slang_code_object_dtr(slang_code_object * self)
    for (i = 0; i < SLANG_BUILTIN_TOTAL; i++)
       _slang_code_unit_dtr(&self->builtin[i]);
    _slang_code_unit_dtr(&self->unit);
+#if 01
    slang_assembly_file_destruct(&self->assembly);
+#endif
    slang_machine_dtr(&self->machine);
    slang_atom_pool_destruct(&self->atompool);
    slang_export_data_table_dtr(&self->expdata);
@@ -233,6 +239,7 @@ typedef struct slang_parse_ctx_
    int parsing_builtin;
    GLboolean global_scope;   /**< Is object being declared a global? */
    slang_atom_pool *atoms;
+   slang_unit_type type;     /**< Vertex vs. Fragment */
 } slang_parse_ctx;
 
 /* slang_output_ctx */
@@ -246,6 +253,7 @@ typedef struct slang_output_ctx_
    slang_var_pool *global_pool;
    slang_machine *machine;
    struct gl_program *program;
+   slang_var_table *vartable;
 } slang_output_ctx;
 
 /* _slang_compile() */
@@ -682,7 +690,7 @@ parse_type_specifier(slang_parse_ctx * C, slang_output_ctx * O,
 
          stru = slang_struct_scope_find(O->structs, a_name, 1);
          if (stru == NULL) {
-            slang_info_log_error(C->L, "%s: undeclared type name.",
+            slang_info_log_error(C->L, "undeclared type name '%s'",
                                  slang_atom_pool_id(C->atoms, a_name));
             return 0;
          }
@@ -714,7 +722,9 @@ parse_fully_specified_type(slang_parse_ctx * C, slang_output_ctx * O,
 {
    if (!parse_type_qualifier(C, &type->qualifier))
       return 0;
-   return parse_type_specifier(C, O, &type->specifier);
+   if (!parse_type_specifier(C, O, &type->specifier))
+      return 0;
+   return 1;
 }
 
 /* operation */
@@ -801,25 +811,7 @@ parse_child_operation(slang_parse_ctx * C, slang_output_ctx * O,
    slang_operation *ch;
 
    /* grow child array */
-#if 000
-   oper->children = (slang_operation *)
-      slang_alloc_realloc(oper->children,
-                          oper->num_children * sizeof(slang_operation),
-                          (oper->num_children + 1) * sizeof(slang_operation));
-   if (oper->children == NULL) {
-      slang_info_log_memory(C->L);
-      return 0;
-   }
-
-   ch = &oper->children[oper->num_children];
-   if (!slang_operation_construct(ch)) {
-      slang_info_log_memory(C->L);
-      return 0;
-   }
-   oper->num_children++;
-#else
    ch = slang_operation_grow(&oper->num_children, &oper->children);
-#endif
    if (statement)
       return parse_statement(C, O, ch);
    return parse_expression(C, O, ch);
@@ -858,44 +850,6 @@ parse_statement(slang_parse_ctx * C, slang_output_ctx * O,
       /* local variable declaration, individual declarators are stored as
        * children identifiers
        */
-#if 000
-      oper->type = slang_oper_variable_decl;
-      {
-         const unsigned int first_var = O->vars->num_variables;
-
-         /* parse the declaration, note that there can be zero or more
-          * than one declarators
-          */
-         if (!parse_declaration(C, O))
-            return 0;
-         if (first_var < O->vars->num_variables) {
-            const unsigned int num_vars = O->vars->num_variables - first_var;
-            unsigned int i;
-
-            oper->children = (slang_operation *)
-               slang_alloc_malloc(num_vars * sizeof(slang_operation));
-            if (oper->children == NULL) {
-               slang_info_log_memory(C->L);
-               return 0;
-            }
-            for (oper->num_children = 0; oper->num_children < num_vars;
-                 oper->num_children++) {
-               if (!slang_operation_construct
-                   (&oper->children[oper->num_children])) {
-                  slang_info_log_memory(C->L);
-                  return 0;
-               }
-            }
-            for (i = first_var; i < O->vars->num_variables; i++) {
-               slang_operation *o = &oper->children[i - first_var];
-               o->type = slang_oper_identifier;
-               o->locals->outer_scope = O->vars;
-               o->a_id = O->vars->variables[i].a_name;
-            }
-         }
-      }
-#else
-
       oper->type = slang_oper_block_no_new_scope;
       {
          const unsigned int first_var = O->vars->num_variables;
@@ -919,13 +873,10 @@ parse_statement(slang_parse_ctx * C, slang_output_ctx * O,
                slang_operation *o = &oper->children[i - first_var];
                o->type = slang_oper_variable_decl;
                o->locals->outer_scope = O->vars;
-               o->a_id = O->vars->variables[i].a_name;
+               o->a_id = O->vars->variables[i]->a_name;
             }
          }
       }
-
-
-#endif
       break;
    case OP_ASM:
       /* the __asm statement, parse the mnemonic and all its arguments
@@ -933,9 +884,6 @@ parse_statement(slang_parse_ctx * C, slang_output_ctx * O,
        */
       oper->type = slang_oper_asm;
       oper->a_id = parse_identifier(C);
-      if (strcmp((char*)oper->a_id, "dot") == 0) {
-         printf("Assemble dot! **************************\n");
-      }
       if (oper->a_id == SLANG_ATOM_NULL)
          return 0;
       while (*C->I != OP_END) {
@@ -1601,15 +1549,19 @@ parse_function_definition(slang_parse_ctx * C, slang_output_ctx * O,
 static GLboolean
 initialize_global(slang_assemble_ctx * A, slang_variable * var)
 {
+#if 01
    slang_assembly_file_restore_point point;
+#endif
    slang_machine mach;
    slang_assembly_local_info save_local = A->local;
    slang_operation op_id, op_assign;
    GLboolean result;
 
+#if 01
    /* save the current assembly */
    if (!slang_assembly_file_restore_point_save(A->file, &point))
       return GL_FALSE;
+#endif
 
    /* setup the machine */
    mach = *A->mach;
@@ -1632,13 +1584,13 @@ initialize_global(slang_assemble_ctx * A, slang_variable * var)
 
    /* put the variable into operation's scope */
    op_id.locals->variables =
-      (slang_variable *) slang_alloc_malloc(sizeof(slang_variable));
+      (slang_variable **) slang_alloc_malloc(sizeof(slang_variable *));
    if (op_id.locals->variables == NULL) {
       slang_operation_destruct(&op_id);
       return GL_FALSE;
    }
    op_id.locals->num_variables = 1;
-   op_id.locals->variables[0] = *var;
+   op_id.locals->variables[0] = var;
 
    /* construct the assignment expression */
    if (!slang_operation_construct(&op_assign)) {
@@ -1659,8 +1611,12 @@ initialize_global(slang_assemble_ctx * A, slang_variable * var)
    op_assign.children[0] = op_id;
    op_assign.children[1] = *var->initializer;
 
+#if 0 /* this should go away */
    /* insert the actual expression */
    result = _slang_assemble_operation(A, &op_assign, slang_ref_forbid);
+#else
+   result = 1;
+#endif
 
    /* carefully destroy the operations */
    op_assign.num_children = 0;
@@ -1676,12 +1632,16 @@ initialize_global(slang_assemble_ctx * A, slang_variable * var)
       return GL_FALSE;
 
    /* execute the expression */
+#if 0
    if (!_slang_execute2(A->file, &mach))
       return GL_FALSE;
+#endif
 
+#if 01
    /* restore the old assembly */
    if (!slang_assembly_file_restore_point_load(A->file, &point))
       return GL_FALSE;
+#endif
    A->local = save_local;
 
    /* now we copy the contents of the initialized variable back to the original machine */
@@ -1774,6 +1734,26 @@ parse_init_declarator(slang_parse_ctx * C, slang_output_ctx * O,
       return 0;
    }
 
+#if 1
+   if (C->global_scope) {
+      slang_assemble_ctx A;
+
+      A.file = O->assembly;
+      A.mach = O->machine;
+      A.atoms = C->atoms;
+      A.space.funcs = O->funs;
+      A.space.structs = O->structs;
+      A.space.vars = O->vars;
+      A.program = O->program;
+#if 0
+      A.codegen = O->codegen;
+#endif
+      A.vartable = O->vartable;
+
+      _slang_codegen_global_variable(&A, var, C->type);
+   }
+#endif
+
    /* allocate global address space for a variable with a known size */
    if (C->global_scope
        && !(var->type.specifier.type == slang_spec_array
@@ -1931,6 +1911,10 @@ parse_function(slang_parse_ctx * C, slang_output_ctx * O, int definition,
       A.space.structs = O->structs;
       A.space.vars = O->vars;
       A.program = O->program;
+#if 0
+      A.codegen = O->codegen;
+#endif
+      A.vartable = O->vartable;
 
       _slang_reset_error();
 
@@ -1940,14 +1924,14 @@ parse_function(slang_parse_ctx * C, slang_output_ctx * O, int definition,
                             (*parsed_func_ret)->param_count);
 #endif
 
-
+#if 0
       if (!_slang_assemble_function(&A, *parsed_func_ret)) {
          /* propogate the error message back through the info log */
          C->L->text = _mesa_strdup(_slang_error_text());
          C->L->dont_free_text = GL_FALSE;
          return GL_FALSE;
       }
-
+#endif
 
 #if 0
       printf("**************************************\n");
@@ -1995,6 +1979,7 @@ parse_code_unit(slang_parse_ctx * C, slang_code_unit * unit,
                 struct gl_program *program)
 {
    slang_output_ctx o;
+   GLboolean success;
 
    /* setup output context */
    o.funs = &unit->funs;
@@ -2004,6 +1989,7 @@ parse_code_unit(slang_parse_ctx * C, slang_code_unit * unit,
    o.global_pool = &unit->object->varpool;
    o.machine = &unit->object->machine;
    o.program = program;
+   o.vartable = _slang_push_var_table(NULL);
 
    /* parse individual functions and declarations */
    while (*C->I != EXTERNAL_NULL) {
@@ -2011,20 +1997,25 @@ parse_code_unit(slang_parse_ctx * C, slang_code_unit * unit,
       case EXTERNAL_FUNCTION_DEFINITION:
          {
             slang_function *func;
-
-            if (!parse_function(C, &o, 1, &func))
-               return GL_FALSE;
+            success = parse_function(C, &o, 1, &func);
          }
          break;
       case EXTERNAL_DECLARATION:
-         if (!parse_declaration(C, &o))
-            return GL_FALSE;
+         success = parse_declaration(C, &o);
          break;
       default:
+         success = GL_FALSE;
+      }
+
+      if (!success) {
+         /* xxx free codegen */
+         _slang_pop_var_table(o.vartable);
          return GL_FALSE;
       }
    }
    C->I++;
+
+   _slang_pop_var_table(o.vartable);
    return GL_TRUE;
 }
 
@@ -2044,6 +2035,7 @@ compile_binary(const byte * prod, slang_code_unit * unit,
    C.parsing_builtin = (builtin == NULL);
    C.global_scope = GL_TRUE;
    C.atoms = &unit->object->atompool;
+   C.type = type;
 
    if (!check_revision(&C))
       return GL_FALSE;
@@ -2097,7 +2089,7 @@ compile_with_grammar(grammar id, const char *source, slang_code_unit * unit,
       slang_string_free(&preprocessed);
       grammar_get_last_error((byte *) (buf), sizeof(buf), &pos);
       slang_info_log_error(infolog, buf);
-      RETURN_ERROR("syntax error", 0);
+      RETURN_ERROR("syntax error (possibly in library code)", 0);
    }
    slang_string_free(&preprocessed);
 
@@ -2223,6 +2215,7 @@ compile_object(grammar * id, const char *source, slang_code_object * object,
 }
 
 
+#if 0
 static void
 slang_create_uniforms(const slang_export_data_table *exports,
                       struct gl_program *program)
@@ -2237,20 +2230,24 @@ slang_create_uniforms(const slang_export_data_table *exports,
       }
    }
 }
+#endif
 
 
-GLboolean
-_slang_compile(const char *source, slang_code_object * object,
+static GLboolean
+compile_shader(GLcontext *ctx, slang_code_object * object,
                slang_unit_type type, slang_info_log * infolog,
-               struct gl_program *program)
+               struct gl_shader *shader)
 {
+   struct gl_program *program = shader->Programs[0];
    GLboolean success;
    grammar id = 0;
 
+   assert(program);
+
    _slang_code_object_dtr(object);
    _slang_code_object_ctr(object);
 
-   success = compile_object(&id, source, object, type, infolog, program);
+   success = compile_object(&id, shader->Source, object, type, infolog, program);
    if (id != 0)
       grammar_destroy(id);
    if (!success)
@@ -2264,22 +2261,76 @@ _slang_compile(const char *source, slang_code_object * object,
 
 #if NEW_SLANG
    {
-      GET_CURRENT_CONTEXT(ctx);
-      slang_create_uniforms(&object->expdata, program);
+      slang_create_uniforms(&object->expdata, shader);
       _mesa_print_program(program);
       _mesa_print_program_parameters(ctx, program);
    }
 #endif
 
+   return GL_TRUE;
+}
+
 
-#if defined(USE_X86_ASM) || defined(SLANG_X86)
-   /* XXX: lookup the @main label */
-   if (!_slang_x86_codegen
-       (&object->machine, &object->assembly,
-        object->expcode.entries[0].address))
-      return GL_FALSE;
+
+GLboolean
+_slang_compile(GLcontext *ctx, struct gl_shader *shader)
+{
+   GLboolean success;
+   slang_info_log info_log;
+   slang_code_object obj;
+   slang_unit_type type;
+
+   if (shader->Type == GL_VERTEX_SHADER) {
+      type = slang_unit_vertex_shader;
+   }
+   else {
+      assert(shader->Type == GL_FRAGMENT_SHADER);
+      type = slang_unit_fragment_shader;
+   }
+
+   /* XXX temporary hack */
+   if (!shader->Programs) {
+      GLenum progTarget;
+      if (shader->Type == GL_VERTEX_SHADER)
+         progTarget = GL_VERTEX_PROGRAM_ARB;
+      else
+         progTarget = GL_FRAGMENT_PROGRAM_ARB;
+      shader->Programs
+         = (struct gl_program **) malloc(sizeof(struct gl_program*));
+      shader->Programs[0] = _mesa_new_program(ctx, progTarget, 1);
+      shader->NumPrograms = 1;
+
+      shader->Programs[0]->Parameters = _mesa_new_parameter_list();
+      shader->Programs[0]->Varying = _mesa_new_parameter_list();
+      shader->Programs[0]->Attributes = _mesa_new_parameter_list();
+   }
+
+   slang_info_log_construct(&info_log);
+   _slang_code_object_ctr(&obj);
+
+   success = compile_shader(ctx, &obj, type, &info_log, shader);
+
+   if (success) {
+#if 0
+      slang_create_uniforms(&object->expdata, shader);
+      _mesa_print_program(program);
+      _mesa_print_program_parameters(ctx, program);
 #endif
+   }
+   else {
+      /* XXX more work on info log needed here */
+      if (info_log.text) {
+         if (shader->InfoLog) {
+            free(shader->InfoLog);
+            shader->InfoLog = NULL;
+         }
+         shader->InfoLog = strdup(info_log.text);
+      }
+   }
 
-   return GL_TRUE;
+   slang_info_log_destruct(&info_log);
+   _slang_code_object_dtr(&obj);
+
+   return success;
 }