* slang front-end compiler
* \author Michal Krol
*/
-
-#include "imports.h"
-#include "grammar_mesa.h"
+\r
+#include "imports.h"\r
+#include "grammar_mesa.h"\r
#include "slang_utility.h"
#include "slang_compile.h"
#include "slang_preprocess.h"
-#include "slang_storage.h"
-#include "slang_assemble.h"
-#include "slang_execute.h"
+#include "slang_storage.h"\r
+#include "slang_assemble.h"\r
+#include "slang_execute.h"\r
/*
This is a straightforward implementation of the slang front-end compiler.
break;
default:
return 0;
- }
+ }\r
func->parameters->outer_scope = scope;
while (*C->I++ == PARAMETER_NEXT)
{
/* assemble the parsed function */
if (definition)
{
-static int x = 0;
-static
slang_assembly_file file;
slang_assembly_name_space space;
-x++;
-if (x == 1)
+\r
slang_assembly_file_construct (&file);
space.funcs = funcs;
space.structs = structs;
space.vars = scope;
-if (x == 1)
-xxx_first (&file);
+
(**parsed_func_ret).address = file.count;
if (!_slang_assemble_function (&file, *parsed_func_ret, &space))
{
slang_assembly_file_destruct (&file);
return 0;
}
-if (slang_string_compare ("main", (**parsed_func_ret).header.name) == 0)
-{
-xxx_prolog (&file, (**parsed_func_ret).address);
-_slang_execute (&file);
-slang_assembly_file_destruct (&file);
-_mesa_exit (0);
-}
}
return 1;
}
static const char *slang_shader_syn =
#include "library/slang_shader_syn.h"
;
-/*
-static const byte slang_core_gc_bin[] = {
-#include "library/slang_core_gc_bin.h"
-};*/
+
static const byte slang_core_gc[] = {
#include "library/slang_core_gc.h"
};
-static const byte slang_common_builtin_gc_bin[] = {
-#include "library/slang_common_builtin_gc_bin.h"
+static const byte slang_common_builtin_gc[] = {
+#include "library/slang_common_builtin_gc.h"
};
-static const byte slang_fragment_builtin_gc_bin[] = {
-#include "library/slang_fragment_builtin_gc_bin.h"
+static const byte slang_fragment_builtin_gc[] = {
+#include "library/slang_fragment_builtin_gc.h"
};
-static const byte slang_vertex_builtin_gc_bin[] = {
-#include "library/slang_vertex_builtin_gc_bin.h"
-};
+static const byte slang_vertex_builtin_gc[] = {
+#include "library/slang_vertex_builtin_gc.h"
+};\r
+\r
+int compile (grammar *id, slang_translation_unit builtin_units[3], int compiled[3],\r
+ const char *source, slang_translation_unit *unit, slang_unit_type type, slang_info_log *log)\r
+{\r
+ slang_translation_unit *builtins = NULL;\r
+\r
+ /* load slang grammar */\r
+ *id = grammar_load_from_text ((const byte *) (slang_shader_syn));\r
+ if (*id == 0)\r
+ {\r
+ byte buf[1024];\r
+ int pos;\r
+\r
+ grammar_get_last_error (buf, 1024, &pos);\r
+ slang_info_log_error (log, (const char *) (buf));\r
+ return 0;\r
+ }\r
+\r
+ /* set shader type - the syntax is slightly different for different shaders */\r
+ if (type == slang_unit_fragment_shader || type == slang_unit_fragment_builtin)\r
+ grammar_set_reg8 (*id, (const byte *) "shader_type", 1);\r
+ else\r
+ grammar_set_reg8 (*id, (const byte *) "shader_type", 2);\r
+\r
+ /* enable language extensions */\r
+ grammar_set_reg8 (*id, (const byte *) "parsing_builtin", 1);\r
+\r
+ /* if parsing user-specified shader, load built-in library */\r
+ if (type == slang_unit_fragment_shader || type == slang_unit_vertex_shader)\r
+ {\r
+ if (!compile_binary (slang_core_gc, &builtin_units[0], slang_unit_fragment_builtin, log,\r
+ NULL))\r
+ return 0;\r
+ compiled[0] = 1;\r
+\r
+ if (!compile_binary (slang_common_builtin_gc, &builtin_units[1],\r
+ slang_unit_fragment_builtin, log, NULL))\r
+ return 0;\r
+ compiled[1] = 1;\r
+\r
+ if (type == slang_unit_fragment_shader)\r
+ {\r
+ if (!compile_binary (slang_fragment_builtin_gc, &builtin_units[2],\r
+ slang_unit_fragment_builtin, log, NULL))\r
+ return 0;\r
+ }\r
+ else if (type == slang_unit_vertex_shader)\r
+ {\r
+ if (!compile_binary (slang_vertex_builtin_gc, &builtin_units[2],\r
+ slang_unit_vertex_builtin, log, NULL))\r
+ return 0;\r
+ }\r
+ compiled[2] = 1;\r
+\r
+ /* disable language extensions */\r
+ grammar_set_reg8 (*id, (const byte *) "parsing_builtin", 0);\r
+ builtins = builtin_units;\r
+ }\r
+\r
+ /* compile the actual shader - pass-in built-in library for external shader */\r
+ if (!compile_with_grammar (*id, source, unit, type, log, builtins))\r
+ return 0;\r
+\r
+ return 1;\r
+}
int _slang_compile (const char *source, slang_translation_unit *unit, slang_unit_type type,
slang_info_log *log)
-{
- grammar id;
- slang_translation_unit builtin_units[3];
- slang_translation_unit *builtins = NULL;
-
- /* load slang grammar */
- id = grammar_load_from_text ((const byte *) slang_shader_syn);
- if (id == 0)
- {
- char buf[1024];
- unsigned int pos;
- grammar_get_last_error ( (unsigned char*) buf, 1024, (int*) &pos);
- slang_info_log_error (log, buf);
- return 0;
- }
-
- /* set shader type - the syntax is slightly different for different shaders */
- if (type == slang_unit_fragment_shader || type == slang_unit_fragment_builtin)
- grammar_set_reg8 (id, (const byte *) "shader_type", 1);
- else
- grammar_set_reg8 (id, (const byte *) "shader_type", 2);
-
- /* enable language extensions */
- grammar_set_reg8 (id, (const byte *) "parsing_builtin", 1);
-
- /* if parsing user-specified shader, load built-in library */
- if (type == slang_unit_fragment_shader || type == slang_unit_vertex_shader)
- {
- /*if (!compile_binary (slang_core_gc_bin, builtin_units,
- slang_unit_fragment_builtin, log, NULL))*/
- if (!compile_with_grammar (id, (const char*) slang_core_gc, builtin_units, slang_unit_fragment_builtin,
- log, NULL))
- {
- grammar_destroy (id);
- return 0;
- }
- if (!compile_binary (slang_common_builtin_gc_bin, builtin_units + 1,
- slang_unit_fragment_builtin, log, NULL))
- {
- slang_translation_unit_destruct (builtin_units);
- grammar_destroy (id);
- return 0;
- }
- if (type == slang_unit_fragment_shader)
- {
- if (!compile_binary (slang_fragment_builtin_gc_bin, builtin_units + 2,
- slang_unit_fragment_builtin, log, NULL))
- {
- slang_translation_unit_destruct (builtin_units);
- slang_translation_unit_destruct (builtin_units + 1);
- grammar_destroy (id);
- return 0;
- }
- }
- else if (type == slang_unit_vertex_shader)
- {
- if (!compile_binary (slang_vertex_builtin_gc_bin, builtin_units + 2,
- slang_unit_vertex_builtin, log, NULL))
- {
- slang_translation_unit_destruct (builtin_units);
- slang_translation_unit_destruct (builtin_units + 1);
- grammar_destroy (id);
- return 0;
- }
- }
-
- /* disable language extensions */
- grammar_set_reg8 (id, (const byte *) "parsing_builtin", 0);
- builtins = builtin_units;
- }
+{\r
+ int success;
+ grammar id = 0;
+ slang_translation_unit builtin_units[3];\r
+ int compiled[3] = { 0 };
- /* compile the actual shader - pass-in built-in library for external shader */
- if (!compile_with_grammar (id, source, unit, type, log, builtins))
- {
- if (type == slang_unit_fragment_shader || type == slang_unit_vertex_shader)
- {
- slang_translation_unit_destruct (builtin_units);
- slang_translation_unit_destruct (builtin_units + 1);
- slang_translation_unit_destruct (builtin_units + 2);
- }
- grammar_destroy (id);
- return 0;
- }
+ success = compile (&id, builtin_units, compiled, source, unit, type, log);
/* destroy built-in library */
if (type == slang_unit_fragment_shader || type == slang_unit_vertex_shader)
- {
- slang_translation_unit_destruct (builtin_units);
- slang_translation_unit_destruct (builtin_units + 1);
- slang_translation_unit_destruct (builtin_units + 2);
- }
- grammar_destroy (id);
- return 1;
+ {\r
+ int i;\r
+\r
+ for (i = 0; i < 3; i++)\r
+ if (compiled[i] != 0)
+ slang_translation_unit_destruct (&builtin_units[i]);
+ }\r
+ if (id != 0)
+ grammar_destroy (id);\r
+
+ return success;
}
#include "slang_storage.h"\r
#include "slang_execute.h"\r
\r
-#define DEBUG_SLANG\r
+#define DEBUG_SLANG 1\r
+\r
+#if DEBUG_SLANG\r
\r
-#ifdef DEBUG_SLANG\r
static void dump_instruction (FILE *f, slang_assembly *a, unsigned int i)\r
{\r
fprintf (f, "%.5u:\t", i);\r
\r
fclose (f);\r
}\r
+\r
#endif\r
\r
int _slang_execute (const slang_assembly_file *file)\r
{\r
slang_machine mach;\r
+\r
#ifdef DEBUG_SLANG\r
FILE *f;\r
#endif\r
\r
+ /* assume 32-bit floats and uints; should work fine also on 64-bit platforms */\r
+ static_assert(sizeof (GLfloat) == 4);\r
+ static_assert(sizeof (GLuint) == 4);\r
+\r
mach.ip = 0;\r
mach.sp = SLANG_MACHINE_STACK_SIZE;\r
mach.bp = 0;\r
mach.kill = 0;\r
mach.exit = 0;\r
+ mach.global = mach.mem;\r
+ mach.stack = mach.global + SLANG_MACHINE_GLOBAL_SIZE;\r
\r
- /* assume 32-bit machine */\r
- /* XXX why???, disabling the pointer size assertions here.\r
- * See bug 4021.\r
- */\r
- static_assert(sizeof (GLfloat) == 4);\r
- /*static_assert(sizeof (GLfloat *) == 4);*/\r
- static_assert(sizeof (GLuint) == 4);\r
- /*static_assert(sizeof (GLuint *) == 4);*/\r
-\r
-#ifdef DEBUG_SLANG\r
+#if DEBUG_SLANG\r
dump (file);\r
-#endif\r
-\r
-#ifdef DEBUG_SLANG\r
f = fopen ("~mesa-slang-assembly-execution.txt", "w");\r
#endif\r
\r
while (!mach.exit)\r
{\r
- slang_assembly *a = file->code + mach.ip;\r
-#ifdef DEBUG_SLANG\r
+ slang_assembly *a;\r
+\r
+#if DEBUG_SLANG\r
if (f != NULL)\r
{\r
unsigned int i;\r
- dump_instruction (f, a, mach.ip);\r
+\r
+ dump_instruction (f, file->code + mach.ip, mach.ip);\r
fprintf (f, "\t\tsp=%u bp=%u\n", mach.sp, mach.bp);\r
for (i = mach.sp; i < SLANG_MACHINE_STACK_SIZE; i++)\r
- fprintf (f, "\t%.5u\t%6f\t%u\n", i, mach.stack._float[i], mach.stack._addr[i]);\r
+ fprintf (f, "\t%.5u\t%6f\t%u\n", i, mach.stack[i]._float, mach.stack[i]._addr);\r
fflush (f);\r
}\r
#endif\r
+\r
+ a = file->code + mach.ip;\r
mach.ip++;\r
\r
switch (a->type)\r
case slang_asm_float_copy:\r
case slang_asm_int_copy:\r
case slang_asm_bool_copy:\r
- *(mach.stack._floatp[mach.sp + a->param[0] / 4] + a->param[1] / 4) =\r
- mach.stack._float[mach.sp];\r
+ mach.mem[mach.stack[mach.sp + a->param[0] / 4]._addr + a->param[1] / 4]._float =\r
+ mach.stack[mach.sp]._float;\r
mach.sp++;\r
break;\r
case slang_asm_float_move:\r
case slang_asm_int_move:\r
case slang_asm_bool_move:\r
- mach.stack._float[mach.sp + a->param[0] / 4] =\r
- mach.stack._float[mach.sp + (mach.stack._addr[mach.sp] + a->param[1]) / 4];\r
+ mach.stack[mach.sp + a->param[0] / 4]._float =\r
+ mach.stack[mach.sp + (mach.stack[mach.sp]._addr + a->param[1]) / 4]._float;\r
break;\r
case slang_asm_float_push:\r
case slang_asm_int_push:\r
case slang_asm_bool_push:\r
mach.sp--;\r
- mach.stack._float[mach.sp] = a->literal;\r
+ mach.stack[mach.sp]._float = a->literal;\r
break;\r
case slang_asm_float_deref:\r
case slang_asm_int_deref:\r
case slang_asm_bool_deref:\r
- mach.stack._float[mach.sp] = *mach.stack._floatp[mach.sp];\r
+ mach.stack[mach.sp]._float = mach.mem[mach.stack[mach.sp]._addr]._float;\r
break;\r
case slang_asm_float_add:\r
- mach.stack._float[mach.sp + 1] += mach.stack._float[mach.sp];\r
+ mach.stack[mach.sp + 1]._float += mach.stack[mach.sp]._float;\r
mach.sp++;\r
break;\r
case slang_asm_float_multiply:\r
- mach.stack._float[mach.sp + 1] *= mach.stack._float[mach.sp];\r
+ mach.stack[mach.sp + 1]._float *= mach.stack[mach.sp]._float;\r
mach.sp++;\r
break;\r
case slang_asm_float_divide:\r
- mach.stack._float[mach.sp + 1] /= mach.stack._float[mach.sp];\r
+ mach.stack[mach.sp + 1]._float /= mach.stack[mach.sp]._float;\r
mach.sp++;\r
break;\r
case slang_asm_float_negate:\r
- mach.stack._float[mach.sp] = -mach.stack._float[mach.sp];\r
+ mach.stack[mach.sp]._float = -mach.stack[mach.sp]._float;\r
break;\r
case slang_asm_float_less:\r
- mach.stack._float[mach.sp + 1] =\r
- mach.stack._float[mach.sp + 1] < mach.stack._float[mach.sp] ? 1.0f : 0.0f;\r
+ mach.stack[mach.sp + 1]._float =\r
+ mach.stack[mach.sp + 1]._float < mach.stack[mach.sp]._float ? 1.0f : 0.0f;\r
mach.sp++;\r
break;\r
case slang_asm_float_equal:\r
mach.sp--;\r
- mach.stack._float[mach.sp] = mach.stack._float[mach.sp + 1 + a->param[0] / 4] ==\r
- mach.stack._float[mach.sp + 1 + a->param[1] / 4] ? 1.0f : 0.0f;\r
+ mach.stack[mach.sp]._float = mach.stack[mach.sp + 1 + a->param[0] / 4]._float ==\r
+ mach.stack[mach.sp + 1 + a->param[1] / 4]._float ? 1.0f : 0.0f;\r
break;\r
case slang_asm_float_to_int:\r
- mach.stack._float[mach.sp] = (GLfloat) (GLint) mach.stack._float[mach.sp];\r
+ mach.stack[mach.sp]._float = (GLfloat) (GLint) mach.stack[mach.sp]._float;\r
break;\r
case slang_asm_int_to_float:\r
break;\r
case slang_asm_int_to_addr:\r
- mach.stack._addr[mach.sp] = (GLuint) (GLint) mach.stack._float[mach.sp];\r
+ mach.stack[mach.sp]._addr = (GLuint) (GLint) mach.stack[mach.sp]._float;\r
break;\r
case slang_asm_addr_copy:\r
- *mach.stack._addrp[mach.sp + 1] = mach.stack._addr[mach.sp];\r
+ mach.mem[mach.stack[mach.sp + 1]._addr]._addr = mach.stack[mach.sp]._addr;\r
mach.sp++;\r
break;\r
case slang_asm_addr_push:\r
mach.sp--;\r
- mach.stack._addr[mach.sp] = a->param[0];\r
+ mach.stack[mach.sp]._addr = a->param[0];\r
break;\r
case slang_asm_addr_deref:\r
- mach.stack._addr[mach.sp] = *mach.stack._addrp[mach.sp];\r
+ mach.stack[mach.sp]._addr = mach.mem[mach.stack[mach.sp]._addr]._addr;\r
break;\r
case slang_asm_addr_add:\r
- mach.stack._addr[mach.sp + 1] += mach.stack._addr[mach.sp];\r
+ mach.stack[mach.sp + 1]._addr += mach.stack[mach.sp]._addr;\r
mach.sp++;\r
break;\r
case slang_asm_addr_multiply:\r
- mach.stack._addr[mach.sp + 1] *= mach.stack._addr[mach.sp];\r
+ mach.stack[mach.sp + 1]._addr *= mach.stack[mach.sp]._addr;\r
mach.sp++;\r
break;\r
case slang_asm_jump:\r
mach.ip = a->param[0];\r
break;\r
case slang_asm_jump_if_zero:\r
- if (mach.stack._float[mach.sp] == 0.0f)\r
+ if (mach.stack[mach.sp]._float == 0.0f)\r
mach.ip = a->param[0];\r
mach.sp++;\r
break;\r
case slang_asm_enter:\r
mach.sp--;\r
- mach.stack._addr[mach.sp] = mach.bp;\r
+ mach.stack[mach.sp]._addr = mach.bp;\r
mach.bp = mach.sp + a->param[0] / 4;\r
break;\r
case slang_asm_leave:\r
- mach.bp = mach.stack._addr[mach.sp];\r
+ mach.bp = mach.stack[mach.sp]._addr;\r
mach.sp++;\r
break;\r
case slang_asm_local_alloc:\r
break;\r
case slang_asm_local_addr:\r
mach.sp--;\r
- mach.stack._addr[mach.sp] = (GLuint) mach.stack._addr + mach.bp * 4 -\r
+ mach.stack[mach.sp]._addr = SLANG_MACHINE_GLOBAL_SIZE * 4 + mach.bp * 4 -\r
(a->param[0] + a->param[1]) + 4;\r
break;\r
case slang_asm_call:\r
mach.sp--;\r
- mach.stack._addr[mach.sp] = mach.ip;\r
+ mach.stack[mach.sp]._addr = mach.ip;\r
mach.ip = a->param[0];\r
break;\r
case slang_asm_return:\r
- mach.ip = mach.stack._addr[mach.sp];\r
+ mach.ip = mach.stack[mach.sp]._addr;\r
mach.sp++;\r
break;\r
case slang_asm_discard:\r
}\r
}\r
\r
-#ifdef DEBUG_SLANG\r
+#if DEBUG_SLANG\r
if (f != NULL)\r
fclose (f);\r
#endif\r