d3dcompiler: Parse variable declarations.

This commit is contained in:
Matteo Bruni 2012-06-04 17:58:24 +02:00 committed by Alexandre Julliard
parent 2ad2c5fa01
commit 2aa6c460f7
3 changed files with 478 additions and 1 deletions

View File

@ -588,12 +588,131 @@ struct bwriter_shader *SlAssembleShader(const char *text, char **messages) DECLS
HRESULT SlWriteBytecode(const struct bwriter_shader *shader, int dxversion, DWORD **result, DWORD *size) DECLSPEC_HIDDEN;
void SlDeleteShader(struct bwriter_shader *shader) DECLSPEC_HIDDEN;
/* The general IR structure is inspired by Mesa GLSL hir, even though the code
* ends up being quite different in practice. Anyway, here comes the relevant
* licensing information.
*
* Copyright © 2010 Intel Corporation
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice (including the next
* paragraph) shall be included in all copies or substantial portions of the
* Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
enum hlsl_base_type
{
HLSL_TYPE_FLOAT,
HLSL_TYPE_HALF,
HLSL_TYPE_DOUBLE,
HLSL_TYPE_INT,
HLSL_TYPE_UINT,
HLSL_TYPE_BOOL,
HLSL_TYPE_LAST_SCALAR = HLSL_TYPE_BOOL,
HLSL_TYPE_SAMPLER,
HLSL_TYPE_TEXTURE,
HLSL_TYPE_PIXELSHADER,
HLSL_TYPE_VERTEXSHADER,
HLSL_TYPE_STRING,
HLSL_TYPE_STRUCT,
HLSL_TYPE_ARRAY,
HLSL_TYPE_VOID,
};
enum hlsl_matrix_majority
{
HLSL_COLUMN_MAJOR,
HLSL_ROW_MAJOR
};
struct hlsl_type
{
struct list entry;
struct list scope_entry;
enum hlsl_base_type base_type;
const char *name;
unsigned int modifiers;
unsigned int dimx;
unsigned int dimy;
union
{
struct list *elements;
struct
{
struct hlsl_type *type;
unsigned int elements_count;
} array;
} e;
};
struct hlsl_struct_field
{
struct list entry;
struct hlsl_type *type;
const char *name;
const char *semantic;
DWORD modifiers;
};
enum hlsl_ir_node_type
{
HLSL_IR_VAR = 0,
};
struct hlsl_ir_node
{
struct list entry;
enum hlsl_ir_node_type type;
struct hlsl_type *data_type;
char *source_file;
unsigned int line;
unsigned int column;
};
struct hlsl_ir_var
{
struct hlsl_ir_node node;
const char *name;
const char *semantic;
unsigned int modifiers;
struct list scope_entry;
struct hlsl_var_allocation *allocation;
};
struct hlsl_scope
{
struct list entry;
struct list vars;
struct list types;
struct hlsl_scope *upper;
};
/* Structures used only during parsing */
struct parse_variable_def
{
struct list entry;
char *name;
unsigned int array_size;
char *semantic;
struct list *initializer;
};
struct hlsl_parse_ctx
{
char *source_file;
@ -614,12 +733,27 @@ struct hlsl_parse_ctx
extern struct hlsl_parse_ctx hlsl_ctx DECLSPEC_HIDDEN;
void hlsl_message(const char *fmt, ...) PRINTF_ATTR(1,2) DECLSPEC_HIDDEN;
BOOL add_declaration(struct hlsl_scope *scope, struct hlsl_ir_var *decl, BOOL local_var) DECLSPEC_HIDDEN;
struct hlsl_ir_var *get_variable(struct hlsl_scope *scope, const char *name) DECLSPEC_HIDDEN;
void free_declaration(struct hlsl_ir_var *decl) DECLSPEC_HIDDEN;
struct hlsl_type *new_hlsl_type(const char *name, enum hlsl_base_type base_type, unsigned dimx, unsigned dimy) DECLSPEC_HIDDEN;
struct hlsl_type *new_array_type(struct hlsl_type *basic_type, unsigned int array_size) DECLSPEC_HIDDEN;
struct hlsl_type *get_type(struct hlsl_scope *scope, const char *name, BOOL recursive) DECLSPEC_HIDDEN;
BOOL find_function(const char *name) DECLSPEC_HIDDEN;
unsigned int components_count_type(struct hlsl_type *type) DECLSPEC_HIDDEN;
struct hlsl_ir_deref *new_var_deref(struct hlsl_ir_var *var) DECLSPEC_HIDDEN;
void push_scope(struct hlsl_parse_ctx *ctx) DECLSPEC_HIDDEN;
BOOL pop_scope(struct hlsl_parse_ctx *ctx) DECLSPEC_HIDDEN;
struct bwriter_shader *parse_hlsl_shader(const char *text, enum shader_type type, DWORD version,
const char *entrypoint, char **messages) DECLSPEC_HIDDEN;
const char *debug_base_type(const struct hlsl_type *type) DECLSPEC_HIDDEN;
const char *debug_hlsl_type(const struct hlsl_type *type) DECLSPEC_HIDDEN;
const char *debug_modifiers(DWORD modifiers) DECLSPEC_HIDDEN;
void free_hlsl_type(struct hlsl_type *type) DECLSPEC_HIDDEN;
#define MAKE_TAG(ch0, ch1, ch2, ch3) \
((DWORD)(ch0) | ((DWORD)(ch1) << 8) | \
((DWORD)(ch2) << 16) | ((DWORD)(ch3) << 24 ))

View File

@ -47,15 +47,43 @@ static void hlsl_error(const char *s)
set_parse_status(&hlsl_ctx.status, PARSE_ERR);
}
static void debug_dump_decl(struct hlsl_type *type, DWORD modifiers, const char *declname, unsigned int line_no)
{
TRACE("Line %u: ", line_no);
TRACE("%s %s;\n", debug_hlsl_type(type), declname);
}
static BOOL declare_variable(struct hlsl_ir_var *decl, BOOL local)
{
BOOL ret;
TRACE("Declaring variable %s.\n", decl->name);
ret = add_declaration(hlsl_ctx.cur_scope, decl, local);
if (ret == FALSE)
{
struct hlsl_ir_var *old = get_variable(hlsl_ctx.cur_scope, decl->name);
hlsl_message("Line %u: \"%s\" already declared.\n", hlsl_ctx.line_no, decl->name);
hlsl_message("Line %u: \"%s\" was previously declared here.\n", old->node.line, decl->name);
set_parse_status(&hlsl_ctx.status, PARSE_ERR);
return FALSE;
}
return TRUE;
}
%}
%error-verbose
%union
{
char *name;
struct hlsl_type *type;
INT intval;
FLOAT floatval;
char *name;
DWORD modifiers;
struct list *list;
struct parse_variable_def *variable_def;
}
%token KW_BLENDSTATE
@ -160,14 +188,26 @@ static void hlsl_error(const char *s)
%token <intval> PRE_LINE
%token <name> VAR_IDENTIFIER TYPE_IDENTIFIER NEW_IDENTIFIER
%type <name> any_identifier
%token <name> STRING
%token <floatval> C_FLOAT
%token <intval> C_INTEGER
%type <type> base_type
%type <type> type
%type <modifiers> var_modifiers
%type <intval> array
%type <name> semantic
%type <variable_def> variable_def
%type <list> variables_def
%%
hlsl_prog: /* empty */
{
}
| hlsl_prog declaration_statement
{
TRACE("Declaration statement parsed.\n");
}
| hlsl_prog preproc_directive
{
}
@ -180,17 +220,163 @@ preproc_directive: PRE_LINE STRING
hlsl_ctx.source_file = $2;
}
any_identifier: VAR_IDENTIFIER
| TYPE_IDENTIFIER
| NEW_IDENTIFIER
semantic: /* Empty */
{
$$ = NULL;
}
| ':' any_identifier
{
$$ = $2;
}
type: base_type
{
$$ = $1;
}
base_type: KW_VOID
{
$$ = new_hlsl_type("void", HLSL_TYPE_VOID, 1, 1);
}
| TYPE_IDENTIFIER
{
struct hlsl_type *type;
TRACE("Type %s.\n", $1);
type = get_type(hlsl_ctx.cur_scope, $1, TRUE);
$$ = type;
d3dcompiler_free($1);
}
| KW_STRUCT TYPE_IDENTIFIER
{
struct hlsl_type *type;
TRACE("Struct type %s.\n", $2);
type = get_type(hlsl_ctx.cur_scope, $2, TRUE);
if (type->base_type != HLSL_TYPE_STRUCT)
{
hlsl_message("Line %u: Redefining %s as a structure.\n",
hlsl_ctx.line_no, $2);
set_parse_status(&hlsl_ctx.status, PARSE_ERR);
}
else
{
$$ = type;
}
d3dcompiler_free($2);
}
declaration_statement: declaration
{
}
declaration: var_modifiers type variables_def ';'
{
struct parse_variable_def *v, *v_next;
struct hlsl_ir_var *var;
BOOL ret, local = TRUE;
LIST_FOR_EACH_ENTRY_SAFE(v, v_next, $3, struct parse_variable_def, entry)
{
debug_dump_decl($2, $1, v->name, hlsl_ctx.line_no);
var = d3dcompiler_alloc(sizeof(*var));
var->node.type = HLSL_IR_VAR;
if (v->array_size)
var->node.data_type = new_array_type($2, v->array_size);
else
var->node.data_type = $2;
var->name = v->name;
var->modifiers = $1;
var->semantic = v->semantic;
var->node.line = hlsl_ctx.line_no;
if (v->initializer)
{
FIXME("Variable with an initializer.\n");
}
ret = declare_variable(var, local);
if (ret == FALSE)
free_declaration(var);
else
TRACE("Declared variable %s.\n", var->name);
d3dcompiler_free(v);
}
d3dcompiler_free($3);
}
variables_def: variable_def
{
$$ = d3dcompiler_alloc(sizeof(*$$));
list_init($$);
list_add_head($$, &$1->entry);
}
| variables_def ',' variable_def
{
$$ = $1;
list_add_tail($$, &$3->entry);
}
/* FIXME: Local variables can't have semantics. */
variable_def: any_identifier array semantic
{
$$ = d3dcompiler_alloc(sizeof(*$$));
$$->name = $1;
$$->array_size = $2;
$$->semantic = $3;
}
array: /* Empty */
{
$$ = 0;
}
var_modifiers: /* Empty */
{
$$ = 0;
}
%%
struct bwriter_shader *parse_hlsl(enum shader_type type, DWORD version, const char *entrypoint, char **messages)
{
struct hlsl_scope *scope, *next_scope;
struct hlsl_type *hlsl_type, *next_type;
struct hlsl_ir_var *var, *next_var;
hlsl_ctx.line_no = 1;
hlsl_ctx.source_file = d3dcompiler_strdup("");
hlsl_ctx.cur_scope = NULL;
hlsl_ctx.matrix_majority = HLSL_COLUMN_MAJOR;
list_init(&hlsl_ctx.scopes);
list_init(&hlsl_ctx.types);
list_init(&hlsl_ctx.functions);
push_scope(&hlsl_ctx);
hlsl_ctx.globals = hlsl_ctx.cur_scope;
hlsl_parse();
d3dcompiler_free(hlsl_ctx.source_file);
TRACE("Freeing variables.\n");
LIST_FOR_EACH_ENTRY_SAFE(scope, next_scope, &hlsl_ctx.scopes, struct hlsl_scope, entry)
{
LIST_FOR_EACH_ENTRY_SAFE(var, next_var, &scope->vars, struct hlsl_ir_var, scope_entry)
{
free_declaration(var);
}
d3dcompiler_free(scope);
}
TRACE("Freeing types.\n");
LIST_FOR_EACH_ENTRY_SAFE(hlsl_type, next_type, &hlsl_ctx.types, struct hlsl_type, entry)
{
free_hlsl_type(hlsl_type);
}
return NULL;
}

View File

@ -761,7 +761,70 @@ void compilation_message(struct compilation_messages *msg, const char *fmt, va_l
}
}
BOOL add_declaration(struct hlsl_scope *scope, struct hlsl_ir_var *decl, BOOL local_var)
{
struct hlsl_ir_var *var;
LIST_FOR_EACH_ENTRY(var, &scope->vars, struct hlsl_ir_var, scope_entry)
{
if (!strcmp(decl->name, var->name))
return FALSE;
}
if (local_var)
{
LIST_FOR_EACH_ENTRY(var, &scope->upper->vars, struct hlsl_ir_var, scope_entry)
{
if (!strcmp(decl->name, var->name))
return FALSE;
}
}
list_add_tail(&scope->vars, &decl->scope_entry);
return TRUE;
}
struct hlsl_ir_var *get_variable(struct hlsl_scope *scope, const char *name)
{
struct hlsl_ir_var *var;
LIST_FOR_EACH_ENTRY(var, &scope->vars, struct hlsl_ir_var, scope_entry)
{
if (!strcmp(name, var->name))
return var;
}
if (!scope->upper)
return NULL;
return get_variable(scope->upper, name);
}
void free_declaration(struct hlsl_ir_var *decl)
{
d3dcompiler_free((void *)decl->name);
d3dcompiler_free((void *)decl->semantic);
d3dcompiler_free(decl);
}
struct hlsl_type *new_hlsl_type(const char *name, enum hlsl_base_type base_type, unsigned dimx, unsigned dimy)
{
struct hlsl_type *type;
type = d3dcompiler_alloc(sizeof(*type));
if (!type)
{
ERR("Out of memory\n");
return NULL;
}
type->name = name;
type->base_type = base_type;
type->dimx = dimx;
type->dimy = dimy;
list_add_tail(&hlsl_ctx.types, &type->entry);
return type;
}
struct hlsl_type *new_array_type(struct hlsl_type *basic_type, unsigned int array_size)
{
FIXME("stub.\n");
return NULL;
@ -778,3 +841,97 @@ BOOL find_function(const char *name)
FIXME("stub.\n");
return FALSE;
}
void push_scope(struct hlsl_parse_ctx *ctx)
{
struct hlsl_scope *new_scope = d3dcompiler_alloc(sizeof(*new_scope));
if (!new_scope)
{
ERR("Out of memory!\n");
return;
}
list_init(&new_scope->vars);
list_init(&new_scope->types);
new_scope->upper = ctx->cur_scope;
ctx->cur_scope = new_scope;
list_add_tail(&ctx->scopes, &new_scope->entry);
}
BOOL pop_scope(struct hlsl_parse_ctx *ctx)
{
struct hlsl_scope *prev_scope = ctx->cur_scope->upper;
if (!prev_scope)
return FALSE;
ctx->cur_scope = prev_scope;
return TRUE;
}
const char *debug_base_type(const struct hlsl_type *type)
{
const char *name = "(unknown)";
switch (type->base_type)
{
case HLSL_TYPE_FLOAT: name = "float"; break;
case HLSL_TYPE_HALF: name = "half"; break;
case HLSL_TYPE_INT: name = "int"; break;
case HLSL_TYPE_UINT: name = "uint"; break;
case HLSL_TYPE_BOOL: name = "bool"; break;
case HLSL_TYPE_STRUCT: name = "struct"; break;
case HLSL_TYPE_ARRAY: name = "array"; break;
default:
FIXME("Unhandled case %u\n", type->base_type);
}
return name;
}
const char *debug_hlsl_type(const struct hlsl_type *type)
{
const char *name;
if (type->name)
return debugstr_a(type->name);
if (type->base_type == HLSL_TYPE_STRUCT)
name = "<anonymous struct>";
else
name = debug_base_type(type);
if (type->dimx == 1 && type->dimy == 1)
return wine_dbg_sprintf("%s", name);
if (type->dimy == 1)
return wine_dbg_sprintf("vector<%s, %u>", name, type->dimx);
return wine_dbg_sprintf("matrix<%s, %u, %u>", name, type->dimx, type->dimy);
}
const char *debug_node_type(enum hlsl_ir_node_type type)
{
const char *names[] =
{
"HLSL_IR_VAR",
};
if (type > sizeof(names) / sizeof(names[0]))
{
return "Unexpected node type";
}
return names[type];
}
void free_hlsl_type(struct hlsl_type *type)
{
struct hlsl_struct_field *field, *next_field;
d3dcompiler_free((void *)type->name);
if (type->base_type == HLSL_TYPE_STRUCT)
{
LIST_FOR_EACH_ENTRY_SAFE(field, next_field, type->e.elements, struct hlsl_struct_field, entry)
{
d3dcompiler_free((void *)field->name);
d3dcompiler_free((void *)field->semantic);
d3dcompiler_free(field);
}
}
d3dcompiler_free(type);
}