| 1 |
/* |
| 2 |
* Mesa 3-D graphics library |
| 3 |
* Version: 6.3 |
| 4 |
* |
| 5 |
* Copyright (C) 2005 Brian Paul All Rights Reserved. |
| 6 |
* |
| 7 |
* Permission is hereby granted, free of charge, to any person obtaining a |
| 8 |
* copy of this software and associated documentation files (the "Software"), |
| 9 |
* to deal in the Software without restriction, including without limitation |
| 10 |
* the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 11 |
* and/or sell copies of the Software, and to permit persons to whom the |
| 12 |
* Software is furnished to do so, subject to the following conditions: |
| 13 |
* |
| 14 |
* The above copyright notice and this permission notice shall be included |
| 15 |
* in all copies or substantial portions of the Software. |
| 16 |
* |
| 17 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
| 18 |
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 19 |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 20 |
* BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN |
| 21 |
* AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 22 |
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 23 |
*/ |
| 24 |
|
| 25 |
/** |
| 26 |
* \file slang_compile.c |
| 27 |
* slang front-end compiler |
| 28 |
* \author Michal Krol |
| 29 |
*/ |
| 30 |
|
| 31 |
#include "imports.h" |
| 32 |
#include "grammar_mesa.h" |
| 33 |
#include "slang_utility.h" |
| 34 |
#include "slang_compile.h" |
| 35 |
#include "slang_preprocess.h" |
| 36 |
#include "slang_storage.h" |
| 37 |
#include "slang_assemble.h" |
| 38 |
#include "slang_execute.h" |
| 39 |
|
| 40 |
/* |
| 41 |
This is a straightforward implementation of the slang front-end compiler. |
| 42 |
Lots of error-checking functionality is missing but every well-formed shader source should |
| 43 |
compile successfully and execute as expected. However, some semantically ill-formed shaders |
| 44 |
may be accepted resulting in undefined behaviour. |
| 45 |
*/ |
| 46 |
|
| 47 |
static void slang_variable_construct (slang_variable *); |
| 48 |
static int slang_variable_copy (slang_variable *, const slang_variable *); |
| 49 |
static void slang_struct_destruct (slang_struct *); |
| 50 |
static int slang_struct_equal (const slang_struct *, const slang_struct *); |
| 51 |
static void slang_variable_destruct (slang_variable *); |
| 52 |
|
| 53 |
/* slang_type_specifier_type */ |
| 54 |
|
| 55 |
/* these must match with slang_type_specifier_type enum */ |
| 56 |
static const char *type_specifier_type_names[] = { |
| 57 |
"void", |
| 58 |
"bool", |
| 59 |
"bvec2", |
| 60 |
"bvec3", |
| 61 |
"bvec4", |
| 62 |
"int", |
| 63 |
"ivec2", |
| 64 |
"ivec3", |
| 65 |
"ivec4", |
| 66 |
"float", |
| 67 |
"vec2", |
| 68 |
"vec3", |
| 69 |
"vec4", |
| 70 |
"mat2", |
| 71 |
"mat3", |
| 72 |
"mat4", |
| 73 |
"sampler1D", |
| 74 |
"sampler2D", |
| 75 |
"sampler3D", |
| 76 |
"samplerCube", |
| 77 |
"sampler1DShadow", |
| 78 |
"sampler2DShadow", |
| 79 |
NULL |
| 80 |
}; |
| 81 |
|
| 82 |
slang_type_specifier_type slang_type_specifier_type_from_string (const char *name) |
| 83 |
{ |
| 84 |
const char **p = type_specifier_type_names; |
| 85 |
while (*p != NULL) |
| 86 |
{ |
| 87 |
if (slang_string_compare (*p, name) == 0) |
| 88 |
return (slang_type_specifier_type) (p - type_specifier_type_names); |
| 89 |
p++; |
| 90 |
} |
| 91 |
return slang_spec_void; |
| 92 |
} |
| 93 |
|
| 94 |
/* slang_type_specifier */ |
| 95 |
|
| 96 |
void slang_type_specifier_construct (slang_type_specifier *spec) |
| 97 |
{ |
| 98 |
spec->type = slang_spec_void; |
| 99 |
spec->_struct = NULL; |
| 100 |
spec->_array = NULL; |
| 101 |
} |
| 102 |
|
| 103 |
void slang_type_specifier_destruct (slang_type_specifier *spec) |
| 104 |
{ |
| 105 |
if (spec->_struct != NULL) |
| 106 |
{ |
| 107 |
slang_struct_destruct (spec->_struct); |
| 108 |
slang_alloc_free (spec->_struct); |
| 109 |
} |
| 110 |
if (spec->_array != NULL) |
| 111 |
{ |
| 112 |
slang_type_specifier_destruct (spec->_array); |
| 113 |
slang_alloc_free (spec->_array); |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
int slang_type_specifier_copy (slang_type_specifier *x, const slang_type_specifier *y) |
| 118 |
{ |
| 119 |
slang_type_specifier_destruct (x); |
| 120 |
slang_type_specifier_construct (x); |
| 121 |
x->type = y->type; |
| 122 |
if (x->type == slang_spec_struct) |
| 123 |
{ |
| 124 |
x->_struct = (slang_struct *) slang_alloc_malloc (sizeof (slang_struct)); |
| 125 |
if (x->_struct == NULL) |
| 126 |
return 0; |
| 127 |
if (!slang_struct_construct_a (x->_struct)) |
| 128 |
{ |
| 129 |
slang_alloc_free (x->_struct); |
| 130 |
x->_struct = NULL; |
| 131 |
return 0; |
| 132 |
} |
| 133 |
return slang_struct_copy (x->_struct, y->_struct); |
| 134 |
} |
| 135 |
if (x->type == slang_spec_array) |
| 136 |
{ |
| 137 |
x->_array = (slang_type_specifier *) slang_alloc_malloc (sizeof (slang_type_specifier)); |
| 138 |
if (x->_array == NULL) |
| 139 |
return 0; |
| 140 |
slang_type_specifier_construct (x->_array); |
| 141 |
return slang_type_specifier_copy (x->_array, y->_array); |
| 142 |
} |
| 143 |
return 1; |
| 144 |
} |
| 145 |
|
| 146 |
int slang_type_specifier_equal (const slang_type_specifier *x, const slang_type_specifier *y) |
| 147 |
{ |
| 148 |
if (x->type != y->type) |
| 149 |
return 0; |
| 150 |
if (x->type == slang_spec_struct) |
| 151 |
return slang_struct_equal (x->_struct, y->_struct); |
| 152 |
if (x->type == slang_spec_array) |
| 153 |
return slang_type_specifier_equal (x->_array, y->_array); |
| 154 |
return 1; |
| 155 |
} |
| 156 |
|
| 157 |
/* slang_fully_specified_type */ |
| 158 |
|
| 159 |
static void slang_fully_specified_type_construct (slang_fully_specified_type *type) |
| 160 |
{ |
| 161 |
type->qualifier = slang_qual_none; |
| 162 |
slang_type_specifier_construct (&type->specifier); |
| 163 |
} |
| 164 |
|
| 165 |
static void slang_fully_specified_type_destruct (slang_fully_specified_type *type) |
| 166 |
{ |
| 167 |
slang_type_specifier_destruct (&type->specifier); |
| 168 |
} |
| 169 |
|
| 170 |
static int slang_fully_specified_type_copy (slang_fully_specified_type *x, |
| 171 |
const slang_fully_specified_type *y) |
| 172 |
{ |
| 173 |
slang_fully_specified_type_construct (x); |
| 174 |
slang_fully_specified_type_destruct (x); |
| 175 |
x->qualifier = y->qualifier; |
| 176 |
return slang_type_specifier_copy (&x->specifier, &y->specifier); |
| 177 |
} |
| 178 |
|
| 179 |
/* slang_variable_scope */ |
| 180 |
|
| 181 |
static void slang_variable_scope_construct (slang_variable_scope *scope) |
| 182 |
{ |
| 183 |
scope->variables = NULL; |
| 184 |
scope->num_variables = 0; |
| 185 |
scope->outer_scope = NULL; |
| 186 |
} |
| 187 |
|
| 188 |
static void slang_variable_scope_destruct (slang_variable_scope *scope) |
| 189 |
{ |
| 190 |
unsigned int i; |
| 191 |
for (i = 0; i < scope->num_variables; i++) |
| 192 |
slang_variable_destruct (scope->variables + i); |
| 193 |
slang_alloc_free (scope->variables); |
| 194 |
} |
| 195 |
|
| 196 |
static int slang_variable_scope_copy (slang_variable_scope *x, const slang_variable_scope *y) |
| 197 |
{ |
| 198 |
unsigned int i; |
| 199 |
slang_variable_scope_destruct (x); |
| 200 |
slang_variable_scope_construct (x); |
| 201 |
x->variables = (slang_variable *) slang_alloc_malloc (y->num_variables * sizeof ( |
| 202 |
slang_variable)); |
| 203 |
if (x->variables == NULL) |
| 204 |
return 0; |
| 205 |
x->num_variables = y->num_variables; |
| 206 |
for (i = 0; i < x->num_variables; i++) |
| 207 |
slang_variable_construct (x->variables + i); |
| 208 |
for (i = 0; i < x->num_variables; i++) |
| 209 |
if (!slang_variable_copy (x->variables + i, y->variables + i)) |
| 210 |
return 0; |
| 211 |
x->outer_scope = y->outer_scope; |
| 212 |
return 1; |
| 213 |
} |
| 214 |
|
| 215 |
/* slang_operation */ |
| 216 |
|
| 217 |
int slang_operation_construct_a (slang_operation *oper) |
| 218 |
{ |
| 219 |
oper->type = slang_oper_none; |
| 220 |
oper->children = NULL; |
| 221 |
oper->num_children = 0; |
| 222 |
oper->literal = (float) 0; |
| 223 |
oper->identifier = NULL; |
| 224 |
oper->locals = (slang_variable_scope *) slang_alloc_malloc (sizeof (slang_variable_scope)); |
| 225 |
if (oper->locals == NULL) |
| 226 |
return 0; |
| 227 |
slang_variable_scope_construct (oper->locals); |
| 228 |
return 1; |
| 229 |
} |
| 230 |
|
| 231 |
void slang_operation_destruct (slang_operation *oper) |
| 232 |
{ |
| 233 |
unsigned int i; |
| 234 |
for (i = 0; i < oper->num_children; i++) |
| 235 |
slang_operation_destruct (oper->children + i); |
| 236 |
slang_alloc_free (oper->children); |
| 237 |
slang_alloc_free (oper->identifier); |
| 238 |
slang_variable_scope_destruct (oper->locals); |
| 239 |
slang_alloc_free (oper->locals); |
| 240 |
} |
| 241 |
|
| 242 |
static int slang_operation_copy (slang_operation *x, const slang_operation *y) |
| 243 |
{ |
| 244 |
unsigned int i; |
| 245 |
for (i = 0; i < x->num_children; i++) |
| 246 |
slang_operation_destruct (x->children + i); |
| 247 |
slang_alloc_free (x->children); |
| 248 |
x->num_children = 0; |
| 249 |
slang_alloc_free (x->identifier); |
| 250 |
x->identifier = NULL; |
| 251 |
slang_variable_scope_destruct (x->locals); |
| 252 |
slang_variable_scope_construct (x->locals); |
| 253 |
|
| 254 |
x->type = y->type; |
| 255 |
x->children = (slang_operation *) slang_alloc_malloc (y->num_children * sizeof ( |
| 256 |
slang_operation)); |
| 257 |
if (x->children == NULL) |
| 258 |
return 0; |
| 259 |
for (i = 0; i < y->num_children; i++) |
| 260 |
if (!slang_operation_construct_a (x->children + i)) |
| 261 |
{ |
| 262 |
unsigned int j; |
| 263 |
for (j = 0; j < i; j++) |
| 264 |
slang_operation_destruct (x->children + j); |
| 265 |
slang_alloc_free (x->children); |
| 266 |
x->children = NULL; |
| 267 |
return 0; |
| 268 |
} |
| 269 |
x->num_children = y->num_children; |
| 270 |
for (i = 0; i < x->num_children; i++) |
| 271 |
if (!slang_operation_copy (x->children + i, y->children + i)) |
| 272 |
return 0; |
| 273 |
x->literal = y->literal; |
| 274 |
if (y->identifier != NULL) |
| 275 |
{ |
| 276 |
x->identifier = slang_string_duplicate (y->identifier); |
| 277 |
if (x->identifier == NULL) |
| 278 |
return 0; |
| 279 |
} |
| 280 |
if (!slang_variable_scope_copy (x->locals, y->locals)) |
| 281 |
return 0; |
| 282 |
return 1; |
| 283 |
} |
| 284 |
|
| 285 |
/* slang_variable */ |
| 286 |
|
| 287 |
static void slang_variable_construct (slang_variable *var) |
| 288 |
{ |
| 289 |
slang_fully_specified_type_construct (&var->type); |
| 290 |
var->name = NULL; |
| 291 |
var->array_size = NULL; |
| 292 |
var->initializer = NULL; |
| 293 |
var->address = ~0; |
| 294 |
} |
| 295 |
|
| 296 |
static void slang_variable_destruct (slang_variable *var) |
| 297 |
{ |
| 298 |
slang_fully_specified_type_destruct (&var->type); |
| 299 |
slang_alloc_free (var->name); |
| 300 |
if (var->array_size != NULL) |
| 301 |
{ |
| 302 |
slang_operation_destruct (var->array_size); |
| 303 |
slang_alloc_free (var->array_size); |
| 304 |
} |
| 305 |
if (var->initializer != NULL) |
| 306 |
{ |
| 307 |
slang_operation_destruct (var->initializer); |
| 308 |
slang_alloc_free (var->initializer); |
| 309 |
} |
| 310 |
} |
| 311 |
|
| 312 |
static int slang_variable_copy (slang_variable *x, const slang_variable *y) |
| 313 |
{ |
| 314 |
slang_variable_destruct (x); |
| 315 |
slang_variable_construct (x); |
| 316 |
if (!slang_fully_specified_type_copy (&x->type, &y->type)) |
| 317 |
return 0; |
| 318 |
if (y->name != NULL) |
| 319 |
{ |
| 320 |
x->name = slang_string_duplicate (y->name); |
| 321 |
if (x->name == NULL) |
| 322 |
return 0; |
| 323 |
} |
| 324 |
if (y->array_size != NULL) |
| 325 |
{ |
| 326 |
x->array_size = (slang_operation *) slang_alloc_malloc (sizeof (slang_operation)); |
| 327 |
if (x->array_size == NULL) |
| 328 |
return 0; |
| 329 |
if (!slang_operation_construct_a (x->array_size)) |
| 330 |
{ |
| 331 |
slang_alloc_free (x->array_size); |
| 332 |
x->array_size = NULL; |
| 333 |
return 0; |
| 334 |
} |
| 335 |
if (!slang_operation_copy (x->array_size, y->array_size)) |
| 336 |
return 0; |
| 337 |
} |
| 338 |
if (y->initializer != NULL) |
| 339 |
{ |
| 340 |
x->initializer = (slang_operation *) slang_alloc_malloc (sizeof (slang_operation)); |
| 341 |
if (x->initializer == NULL) |
| 342 |
return 0; |
| 343 |
if (!slang_operation_construct_a (x->initializer)) |
| 344 |
{ |
| 345 |
slang_alloc_free (x->initializer); |
| 346 |
x->initializer = NULL; |
| 347 |
return 0; |
| 348 |
} |
| 349 |
if (!slang_operation_copy (x->initializer, y->initializer)) |
| 350 |
return 0; |
| 351 |
} |
| 352 |
return 1; |
| 353 |
} |
| 354 |
|
| 355 |
slang_variable *_slang_locate_variable (slang_variable_scope *scope, const char *name, int all) |
| 356 |
{ |
| 357 |
unsigned int i; |
| 358 |
for (i = 0; i < scope->num_variables; i++) |
| 359 |
if (slang_string_compare (name, scope->variables[i].name) == 0) |
| 360 |
return scope->variables + i; |
| 361 |
if (all && scope->outer_scope != NULL) |
| 362 |
return _slang_locate_variable (scope->outer_scope, name, 1); |
| 363 |
return NULL; |
| 364 |
} |
| 365 |
|
| 366 |
/* slang_struct_scope */ |
| 367 |
|
| 368 |
static void slang_struct_scope_construct (slang_struct_scope *scope) |
| 369 |
{ |
| 370 |
scope->structs = NULL; |
| 371 |
scope->num_structs = 0; |
| 372 |
scope->outer_scope = NULL; |
| 373 |
} |
| 374 |
|
| 375 |
static void slang_struct_scope_destruct (slang_struct_scope *scope) |
| 376 |
{ |
| 377 |
unsigned int i; |
| 378 |
for (i = 0; i < scope->num_structs; i++) |
| 379 |
slang_struct_destruct (scope->structs + i); |
| 380 |
slang_alloc_free (scope->structs); |
| 381 |
} |
| 382 |
|
| 383 |
static int slang_struct_scope_copy (slang_struct_scope *x, const slang_struct_scope *y) |
| 384 |
{ |
| 385 |
unsigned int i; |
| 386 |
slang_struct_scope_destruct (x); |
| 387 |
slang_struct_scope_construct (x); |
| 388 |
x->structs = (slang_struct *) slang_alloc_malloc (y->num_structs * sizeof (slang_struct)); |
| 389 |
if (x->structs == NULL) |
| 390 |
return 0; |
| 391 |
x->num_structs = y->num_structs; |
| 392 |
for (i = 0; i < x->num_structs; i++) |
| 393 |
{ |
| 394 |
unsigned int j; |
| 395 |
if (!slang_struct_construct_a (x->structs + i)) |
| 396 |
{ |
| 397 |
for (j = 0; j < i; j++) |
| 398 |
slang_struct_destruct (x->structs + j); |
| 399 |
slang_alloc_free (x->structs); |
| 400 |
x->structs = NULL; |
| 401 |
return 0; |
| 402 |
} |
| 403 |
} |
| 404 |
for (i = 0; i < x->num_structs; i++) |
| 405 |
if (!slang_struct_copy (x->structs + i, y->structs + i)) |
| 406 |
return 0; |
| 407 |
x->outer_scope = y->outer_scope; |
| 408 |
return 1; |
| 409 |
} |
| 410 |
|
| 411 |
slang_struct *slang_struct_scope_find (slang_struct_scope *stru, const char *name, int all_scopes) |
| 412 |
{ |
| 413 |
unsigned int i; |
| 414 |
for (i = 0; i < stru->num_structs; i++) |
| 415 |
if (slang_string_compare (name, stru->structs[i].name) == 0) |
| 416 |
return stru->structs + i; |
| 417 |
if (all_scopes && stru->outer_scope != NULL) |
| 418 |
return slang_struct_scope_find (stru->outer_scope, name, 1); |
| 419 |
return NULL; |
| 420 |
} |
| 421 |
|
| 422 |
/* slang_struct */ |
| 423 |
|
| 424 |
int slang_struct_construct_a (slang_struct *stru) |
| 425 |
{ |
| 426 |
stru->name = NULL; |
| 427 |
stru->fields = (slang_variable_scope *) slang_alloc_malloc (sizeof (slang_variable_scope)); |
| 428 |
if (stru->fields == NULL) |
| 429 |
return 0; |
| 430 |
slang_variable_scope_construct (stru->fields); |
| 431 |
stru->structs = (slang_struct_scope *) slang_alloc_malloc (sizeof (slang_struct_scope)); |
| 432 |
if (stru->structs == NULL) |
| 433 |
{ |
| 434 |
slang_variable_scope_destruct (stru->fields); |
| 435 |
slang_alloc_free (stru->fields); |
| 436 |
return 0; |
| 437 |
} |
| 438 |
slang_struct_scope_construct (stru->structs); |
| 439 |
return 1; |
| 440 |
} |
| 441 |
|
| 442 |
static void slang_struct_destruct (slang_struct *stru) |
| 443 |
{ |
| 444 |
slang_alloc_free (stru->name); |
| 445 |
slang_variable_scope_destruct (stru->fields); |
| 446 |
slang_alloc_free (stru->fields); |
| 447 |
slang_struct_scope_destruct (stru->structs); |
| 448 |
slang_alloc_free (stru->structs); |
| 449 |
} |
| 450 |
|
| 451 |
int slang_struct_copy (slang_struct *x, const slang_struct *y) |
| 452 |
{ |
| 453 |
slang_alloc_free (x->name); |
| 454 |
x->name = NULL; |
| 455 |
slang_variable_scope_destruct (x->fields); |
| 456 |
slang_variable_scope_construct (x->fields); |
| 457 |
slang_struct_scope_destruct (x->structs); |
| 458 |
slang_struct_scope_construct (x->structs); |
| 459 |
if (y->name != NULL) |
| 460 |
{ |
| 461 |
x->name = slang_string_duplicate (y->name); |
| 462 |
if (x->name == NULL) |
| 463 |
return 0; |
| 464 |
} |
| 465 |
if (!slang_variable_scope_copy (x->fields, y->fields)) |
| 466 |
return 0; |
| 467 |
if (!slang_struct_scope_copy (x->structs, y->structs)) |
| 468 |
return 0; |
| 469 |
return 1; |
| 470 |
} |
| 471 |
|
| 472 |
static int slang_struct_equal (const slang_struct *x, const slang_struct *y) |
| 473 |
{ |
| 474 |
unsigned int i; |
| 475 |
if (x->fields->num_variables != y->fields->num_variables) |
| 476 |
return 0; |
| 477 |
for (i = 0; i < x->fields->num_variables; i++) |
| 478 |
{ |
| 479 |
slang_variable *varx = x->fields->variables + i; |
| 480 |
slang_variable *vary = y->fields->variables + i; |
| 481 |
if (slang_string_compare (varx->name, vary->name) != 0) |
| 482 |
return 0; |
| 483 |
if (!slang_type_specifier_equal (&varx->type.specifier, &vary->type.specifier)) |
| 484 |
return 0; |
| 485 |
if (varx->type.specifier.type == slang_spec_array) |
| 486 |
{ |
| 487 |
/* TODO compare array sizes */ |
| 488 |
} |
| 489 |
} |
| 490 |
return 1; |
| 491 |
} |
| 492 |
|
| 493 |
/* slang_function */ |
| 494 |
/* XXX mem! */ |
| 495 |
static void slang_function_construct (slang_function *func) |
| 496 |
{ |
| 497 |
func->kind = slang_func_ordinary; |
| 498 |
slang_variable_construct (&func->header); |
| 499 |
func->parameters = (slang_variable_scope *) slang_alloc_malloc (sizeof (slang_variable_scope)); |
| 500 |
slang_variable_scope_construct (func->parameters); |
| 501 |
func->body = NULL; |
| 502 |
func->address = ~0; |
| 503 |
} |
| 504 |
|
| 505 |
static void slang_function_destruct (slang_function *func) |
| 506 |
{ |
| 507 |
slang_variable_destruct (&func->header); |
| 508 |
slang_variable_scope_destruct (func->parameters); |
| 509 |
slang_alloc_free (func->parameters); |
| 510 |
if (func->body != NULL) |
| 511 |
{ |
| 512 |
slang_operation_destruct (func->body); |
| 513 |
slang_alloc_free (func->body); |
| 514 |
} |
| 515 |
} |
| 516 |
|
| 517 |
/* slang_function_scope */ |
| 518 |
|
| 519 |
static void slang_function_scope_construct (slang_function_scope *scope) |
| 520 |
{ |
| 521 |
scope->functions = NULL; |
| 522 |
scope->num_functions = 0; |
| 523 |
scope->outer_scope = NULL; |
| 524 |
} |
| 525 |
|
| 526 |
static void slang_function_scope_destruct (slang_function_scope *scope) |
| 527 |
{ |
| 528 |
unsigned int i; |
| 529 |
for (i = 0; i < scope->num_functions; i++) |
| 530 |
slang_function_destruct (scope->functions + i); |
| 531 |
slang_alloc_free (scope->functions); |
| 532 |
} |
| 533 |
|
| 534 |
static int slang_function_scope_find_by_name (slang_function_scope *funcs, const char *name, |
| 535 |
int all_scopes) |
| 536 |
{ |
| 537 |
unsigned int i; |
| 538 |
for (i = 0; i < funcs->num_functions; i++) |
| 539 |
if (slang_string_compare (name, funcs->functions[i].header.name) == 0) |
| 540 |
return 1; |
| 541 |
if (all_scopes && funcs->outer_scope != NULL) |
| 542 |
return slang_function_scope_find_by_name (funcs->outer_scope, name, 1); |
| 543 |
return 0; |
| 544 |
} |
| 545 |
|
| 546 |
static slang_function *slang_function_scope_find (slang_function_scope *funcs, slang_function *fun, |
| 547 |
int all_scopes) |
| 548 |
{ |
| 549 |
unsigned int i; |
| 550 |
for (i = 0; i < funcs->num_functions; i++) |
| 551 |
{ |
| 552 |
slang_function *f = funcs->functions + i; |
| 553 |
unsigned int j; |
| 554 |
if (slang_string_compare (fun->header.name, f->header.name) != 0) |
| 555 |
continue; |
| 556 |
if (fun->param_count != f->param_count) |
| 557 |
continue; |
| 558 |
for (j = 0; j < fun->param_count; j++) |
| 559 |
{ |
| 560 |
if (!slang_type_specifier_equal (&fun->parameters->variables[j].type.specifier, |
| 561 |
&f->parameters->variables[j].type.specifier)) |
| 562 |
{ |
| 563 |
break; |
| 564 |
} |
| 565 |
} |
| 566 |
if (j == fun->param_count) |
| 567 |
return f; |
| 568 |
} |
| 569 |
if (all_scopes && funcs->outer_scope != NULL) |
| 570 |
return slang_function_scope_find (funcs->outer_scope, fun, 1); |
| 571 |
return NULL; |
| 572 |
} |
| 573 |
|
| 574 |
/* slang_translation_unit */ |
| 575 |
|
| 576 |
void slang_translation_unit_construct (slang_translation_unit *unit) |
| 577 |
{ |
| 578 |
slang_variable_scope_construct (&unit->globals); |
| 579 |
slang_function_scope_construct (&unit->functions); |
| 580 |
slang_struct_scope_construct (&unit->structs); |
| 581 |
} |
| 582 |
|
| 583 |
void slang_translation_unit_destruct (slang_translation_unit *unit) |
| 584 |
{ |
| 585 |
slang_variable_scope_destruct (&unit->globals); |
| 586 |
slang_function_scope_destruct (&unit->functions); |
| 587 |
slang_struct_scope_destruct (&unit->structs); |
| 588 |
} |
| 589 |
|
| 590 |
/* slang_info_log */ |
| 591 |
|
| 592 |
static char *out_of_memory = "error: out of memory\n"; |
| 593 |
|
| 594 |
void slang_info_log_construct (slang_info_log *log) |
| 595 |
{ |
| 596 |
log->text = NULL; |
| 597 |
log->dont_free_text = 0; |
| 598 |
} |
| 599 |
|
| 600 |
void slang_info_log_destruct (slang_info_log *log) |
| 601 |
{ |
| 602 |
if (!log->dont_free_text) |
| 603 |
slang_alloc_free (log->text); |
| 604 |
} |
| 605 |
|
| 606 |
static int slang_info_log_message (slang_info_log *log, const char *prefix, const char *msg) |
| 607 |
{ |
| 608 |
unsigned int new_size; |
| 609 |
if (log->dont_free_text) |
| 610 |
return 0; |
| 611 |
new_size = slang_string_length (prefix) + 3 + slang_string_length (msg); |
| 612 |
if (log->text != NULL) |
| 613 |
{ |
| 614 |
log->text = (char *) slang_alloc_realloc (log->text, slang_string_length (log->text) + 1, |
| 615 |
new_size + slang_string_length (log->text) + 1); |
| 616 |
} |
| 617 |
else |
| 618 |
{ |
| 619 |
log->text = (char *) slang_alloc_malloc (new_size + 1); |
| 620 |
if (log->text != NULL) |
| 621 |
*log->text = '\0'; |
| 622 |
} |
| 623 |
if (log->text == NULL) |
| 624 |
return 0; |
| 625 |
slang_string_concat (log->text, prefix); |
| 626 |
slang_string_concat (log->text, ": "); |
| 627 |
slang_string_concat (log->text, msg); |
| 628 |
slang_string_concat (log->text, "\n"); |
| 629 |
return 1; |
| 630 |
} |
| 631 |
|
| 632 |
int slang_info_log_error (slang_info_log *log, const char *msg, ...) |
| 633 |
{ |
| 634 |
va_list va; |
| 635 |
char buf[1024]; |
| 636 |
|
| 637 |
va_start (va, msg); |
| 638 |
vsprintf (buf, msg, va); |
| 639 |
if (slang_info_log_message (log, "error", buf)) |
| 640 |
return 1; |
| 641 |
slang_info_log_memory (log); |
| 642 |
va_end (va); |
| 643 |
return 0; |
| 644 |
} |
| 645 |
|
| 646 |
int slang_info_log_warning (slang_info_log *log, const char *msg, ...) |
| 647 |
{ |
| 648 |
va_list va; |
| 649 |
char buf[1024]; |
| 650 |
|
| 651 |
va_start (va, msg); |
| 652 |
vsprintf (buf, msg, va); |
| 653 |
if (slang_info_log_message (log, "warning", buf)) |
| 654 |
return 1; |
| 655 |
slang_info_log_memory (log); |
| 656 |
va_end (va); |
| 657 |
return 0; |
| 658 |
} |
| 659 |
|
| 660 |
void slang_info_log_memory (slang_info_log *log) |
| 661 |
{ |
| 662 |
if (!slang_info_log_message (log, "error", "out of memory")) |
| 663 |
{ |
| 664 |
log->dont_free_text = 1; |
| 665 |
log->text = out_of_memory; |
| 666 |
} |
| 667 |
} |
| 668 |
|
| 669 |
/* slang_parse_ctx */ |
| 670 |
|
| 671 |
typedef struct slang_parse_ctx_ |
| 672 |
{ |
| 673 |
const byte *I; |
| 674 |
slang_info_log *L; |
| 675 |
int parsing_builtin; |
| 676 |
} slang_parse_ctx; |
| 677 |
|
| 678 |
/* _slang_compile() */ |
| 679 |
|
| 680 |
static int parse_identifier (slang_parse_ctx *C, char **id) |
| 681 |
{ |
| 682 |
*id = slang_string_duplicate ((const char *) C->I); |
| 683 |
if (*id == NULL) |
| 684 |
{ |
| 685 |
slang_info_log_memory (C->L); |
| 686 |
return 0; |
| 687 |
} |
| 688 |
C->I += strlen ((const char *) C->I) + 1; |
| 689 |
return 1; |
| 690 |
} |
| 691 |
|
| 692 |
static int parse_number (slang_parse_ctx *C, int *number) |
| 693 |
{ |
| 694 |
const int radix = (int) (*C->I++); |
| 695 |
*number = 0; |
| 696 |
while (*C->I != '\0') |
| 697 |
{ |
| 698 |
int digit; |
| 699 |
if (*C->I >= '0' && *C->I <= '9') |
| 700 |
digit = (int) (*C->I - '0'); |
| 701 |
else if (*C->I >= 'A' && *C->I <= 'Z') |
| 702 |
digit = (int) (*C->I - 'A') + 10; |
| 703 |
else |
| 704 |
digit = (int) (*C->I - 'a') + 10; |
| 705 |
*number = *number * radix + digit; |
| 706 |
C->I++; |
| 707 |
} |
| 708 |
C->I++; |
| 709 |
if (*number > 65535) |
| 710 |
slang_info_log_warning (C->L, "%d: literal integer overflow", *number); |
| 711 |
return 1; |
| 712 |
} |
| 713 |
|
| 714 |
static int parse_float (slang_parse_ctx *C, float *number) |
| 715 |
{ |
| 716 |
char *integral = NULL; |
| 717 |
char *fractional = NULL; |
| 718 |
char *exponent = NULL; |
| 719 |
char *whole = NULL; |
| 720 |
|
| 721 |
if (!parse_identifier (C, &integral)) |
| 722 |
return 0; |
| 723 |
|
| 724 |
if (!parse_identifier (C, &fractional)) |
| 725 |
{ |
| 726 |
slang_alloc_free (integral); |
| 727 |
return 0; |
| 728 |
} |
| 729 |
|
| 730 |
if (!parse_identifier (C, &exponent)) |
| 731 |
{ |
| 732 |
slang_alloc_free (fractional); |
| 733 |
slang_alloc_free (integral); |
| 734 |
return 0; |
| 735 |
} |
| 736 |
|
| 737 |
whole = (char *) (slang_alloc_malloc ((strlen (integral) + strlen (fractional) + strlen ( |
| 738 |
exponent) + 3) * sizeof (char))); |
| 739 |
if (whole == NULL) |
| 740 |
{ |
| 741 |
slang_alloc_free (exponent); |
| 742 |
slang_alloc_free (fractional); |
| 743 |
slang_alloc_free (integral); |
| 744 |
slang_info_log_memory (C->L); |
| 745 |
return 0; |
| 746 |
} |
| 747 |
|
| 748 |
slang_string_copy (whole, integral); |
| 749 |
slang_string_concat (whole, "."); |
| 750 |
slang_string_concat (whole, fractional); |
| 751 |
slang_string_concat (whole, "E"); |
| 752 |
slang_string_concat (whole, exponent); |
| 753 |
|
| 754 |
*number = (float) (atof (whole)); |
| 755 |
|
| 756 |
slang_alloc_free (whole); |
| 757 |
slang_alloc_free (exponent); |
| 758 |
slang_alloc_free (fractional); |
| 759 |
slang_alloc_free (integral); |
| 760 |
return 1; |
| 761 |
} |
| 762 |
|
| 763 |
/* revision number - increment after each change affecting emitted output */ |
| 764 |
#define REVISION 2 |
| 765 |
|
| 766 |
static int check_revision (slang_parse_ctx *C) |
| 767 |
{ |
| 768 |
if (*C->I != REVISION) |
| 769 |
{ |
| 770 |
slang_info_log_error (C->L, "internal compiler error"); |
| 771 |
return 0; |
| 772 |
} |
| 773 |
C->I++; |
| 774 |
return 1; |
| 775 |
} |
| 776 |
|
| 777 |
static int parse_statement (slang_parse_ctx *, slang_operation *, slang_variable_scope *, |
| 778 |
slang_struct_scope *, slang_function_scope *); |
| 779 |
static int parse_expression (slang_parse_ctx *, slang_operation *, slang_variable_scope *, |
| 780 |
slang_struct_scope *, slang_function_scope *); |
| 781 |
|
| 782 |
/* type qualifier */ |
| 783 |
#define TYPE_QUALIFIER_NONE 0 |
| 784 |
#define TYPE_QUALIFIER_CONST 1 |
| 785 |
#define TYPE_QUALIFIER_ATTRIBUTE 2 |
| 786 |
#define TYPE_QUALIFIER_VARYING 3 |
| 787 |
#define TYPE_QUALIFIER_UNIFORM 4 |
| 788 |
#define TYPE_QUALIFIER_FIXEDOUTPUT 5 |
| 789 |
#define TYPE_QUALIFIER_FIXEDINPUT 6 |
| 790 |
|
| 791 |
static int parse_type_qualifier (slang_parse_ctx *C, slang_type_qualifier *qual) |
| 792 |
{ |
| 793 |
switch (*C->I++) |
| 794 |
{ |
| 795 |
case TYPE_QUALIFIER_NONE: |
| 796 |
*qual = slang_qual_none; |
| 797 |
break; |
| 798 |
case TYPE_QUALIFIER_CONST: |
| 799 |
*qual = slang_qual_const; |
| 800 |
break; |
| 801 |
case TYPE_QUALIFIER_ATTRIBUTE: |
| 802 |
*qual = slang_qual_attribute; |
| 803 |
break; |
| 804 |
case TYPE_QUALIFIER_VARYING: |
| 805 |
*qual = slang_qual_varying; |
| 806 |
break; |
| 807 |
case TYPE_QUALIFIER_UNIFORM: |
| 808 |
*qual = slang_qual_uniform; |
| 809 |
break; |
| 810 |
case TYPE_QUALIFIER_FIXEDOUTPUT: |
| 811 |
*qual = slang_qual_fixedoutput; |
| 812 |
break; |
| 813 |
case TYPE_QUALIFIER_FIXEDINPUT: |
| 814 |
*qual = slang_qual_fixedinput; |
| 815 |
break; |
| 816 |
default: |
| 817 |
return 0; |
| 818 |
} |
| 819 |
return 1; |
| 820 |
} |
| 821 |
|
| 822 |
/* type specifier */ |
| 823 |
#define TYPE_SPECIFIER_VOID 0 |
| 824 |
#define TYPE_SPECIFIER_BOOL 1 |
| 825 |
#define TYPE_SPECIFIER_BVEC2 2 |
| 826 |
#define TYPE_SPECIFIER_BVEC3 3 |
| 827 |
#define TYPE_SPECIFIER_BVEC4 4 |
| 828 |
#define TYPE_SPECIFIER_INT 5 |
| 829 |
#define TYPE_SPECIFIER_IVEC2 6 |
| 830 |
#define TYPE_SPECIFIER_IVEC3 7 |
| 831 |
#define TYPE_SPECIFIER_IVEC4 8 |
| 832 |
#define TYPE_SPECIFIER_FLOAT 9 |
| 833 |
#define TYPE_SPECIFIER_VEC2 10 |
| 834 |
#define TYPE_SPECIFIER_VEC3 11 |
| 835 |
#define TYPE_SPECIFIER_VEC4 12 |
| 836 |
#define TYPE_SPECIFIER_MAT2 13 |
| 837 |
#define TYPE_SPECIFIER_MAT3 14 |
| 838 |
#define TYPE_SPECIFIER_MAT4 15 |
| 839 |
#define TYPE_SPECIFIER_SAMPLER1D 16 |
| 840 |
#define TYPE_SPECIFIER_SAMPLER2D 17 |
| 841 |
#define TYPE_SPECIFIER_SAMPLER3D 18 |
| 842 |
#define TYPE_SPECIFIER_SAMPLERCUBE 19 |
| 843 |
#define TYPE_SPECIFIER_SAMPLER1DSHADOW 20 |
| 844 |
#define TYPE_SPECIFIER_SAMPLER2DSHADOW 21 |
| 845 |
#define TYPE_SPECIFIER_STRUCT 22 |
| 846 |
#define TYPE_SPECIFIER_TYPENAME 23 |
| 847 |
|
| 848 |
/* structure field */ |
| 849 |
#define FIELD_NONE 0 |
| 850 |
#define FIELD_NEXT 1 |
| 851 |
#define FIELD_ARRAY 2 |
| 852 |
|
| 853 |
static int parse_type_specifier (slang_parse_ctx *C, slang_type_specifier *spec, |
| 854 |
slang_struct_scope *structs, slang_variable_scope *scope, slang_function_scope *funcs) |
| 855 |
{ |
| 856 |
switch (*C->I++) |
| 857 |
{ |
| 858 |
case TYPE_SPECIFIER_VOID: |
| 859 |
spec->type = slang_spec_void; |
| 860 |
break; |
| 861 |
case TYPE_SPECIFIER_BOOL: |
| 862 |
spec->type = slang_spec_bool; |
| 863 |
break; |
| 864 |
case TYPE_SPECIFIER_BVEC2: |
| 865 |
spec->type = slang_spec_bvec2; |
| 866 |
break; |
| 867 |
case TYPE_SPECIFIER_BVEC3: |
| 868 |
spec->type = slang_spec_bvec3; |
| 869 |
break; |
| 870 |
case TYPE_SPECIFIER_BVEC4: |
| 871 |
spec->type = slang_spec_bvec4; |
| 872 |
break; |
| 873 |
case TYPE_SPECIFIER_INT: |
| 874 |
spec->type = slang_spec_int; |
| 875 |
break; |
| 876 |
case TYPE_SPECIFIER_IVEC2: |
| 877 |
spec->type = slang_spec_ivec2; |
| 878 |
break; |
| 879 |
case TYPE_SPECIFIER_IVEC3: |
| 880 |
spec->type = slang_spec_ivec3; |
| 881 |
break; |
| 882 |
case TYPE_SPECIFIER_IVEC4: |
| 883 |
spec->type = slang_spec_ivec4; |
| 884 |
break; |
| 885 |
case TYPE_SPECIFIER_FLOAT: |
| 886 |
spec->type = slang_spec_float; |
| 887 |
break; |
| 888 |
case TYPE_SPECIFIER_VEC2: |
| 889 |
spec->type = slang_spec_vec2; |
| 890 |
break; |
| 891 |
case TYPE_SPECIFIER_VEC3: |
| 892 |
spec->type = slang_spec_vec3; |
| 893 |
break; |
| 894 |
case TYPE_SPECIFIER_VEC4: |
| 895 |
spec->type = slang_spec_vec4; |
| 896 |
break; |
| 897 |
case TYPE_SPECIFIER_MAT2: |
| 898 |
spec->type = slang_spec_mat2; |
| 899 |
break; |
| 900 |
case TYPE_SPECIFIER_MAT3: |
| 901 |
spec->type = slang_spec_mat3; |
| 902 |
break; |
| 903 |
case TYPE_SPECIFIER_MAT4: |
| 904 |
spec->type = slang_spec_mat4; |
| 905 |
break; |
| 906 |
case TYPE_SPECIFIER_SAMPLER1D: |
| 907 |
spec->type = slang_spec_sampler1D; |
| 908 |
break; |
| 909 |
case TYPE_SPECIFIER_SAMPLER2D: |
| 910 |
spec->type = slang_spec_sampler2D; |
| 911 |
break; |
| 912 |
case TYPE_SPECIFIER_SAMPLER3D: |
| 913 |
spec->type = slang_spec_sampler3D; |
| 914 |
break; |
| 915 |
case TYPE_SPECIFIER_SAMPLERCUBE: |
| 916 |
spec->type = slang_spec_samplerCube; |
| 917 |
break; |
| 918 |
case TYPE_SPECIFIER_SAMPLER1DSHADOW: |
| 919 |
spec->type = slang_spec_sampler1DShadow; |
| 920 |
break; |
| 921 |
case TYPE_SPECIFIER_SAMPLER2DSHADOW: |
| 922 |
spec->type = slang_spec_sampler2DShadow; |
| 923 |
break; |
| 924 |
case TYPE_SPECIFIER_STRUCT: |
| 925 |
spec->type = slang_spec_struct; |
| 926 |
{ |
| 927 |
char *name; |
| 928 |
if (!parse_identifier (C, &name)) |
| 929 |
return 0; |
| 930 |
if (*name != '\0' && slang_struct_scope_find (structs, name, 0) != NULL) |
| 931 |
{ |
| 932 |
slang_info_log_error (C->L, "%s: duplicate type name", name); |
| 933 |
slang_alloc_free (name); |
| 934 |
return 0; |
| 935 |
} |
| 936 |
spec->_struct = (slang_struct *) slang_alloc_malloc (sizeof (slang_struct)); |
| 937 |
if (spec->_struct == NULL) |
| 938 |
{ |
| 939 |
slang_alloc_free (name); |
| 940 |
slang_info_log_memory (C->L); |
| 941 |
return 0; |
| 942 |
} |
| 943 |
if (!slang_struct_construct_a (spec->_struct)) |
| 944 |
{ |
| 945 |
slang_alloc_free (spec->_struct); |
| 946 |
spec->_struct = NULL; |
| 947 |
slang_alloc_free (name); |
| 948 |
slang_info_log_memory (C->L); |
| 949 |
return 0; |
| 950 |
} |
| 951 |
spec->_struct->name = name; |
| 952 |
spec->_struct->structs->outer_scope = structs; |
| 953 |
} |
| 954 |
do |
| 955 |
{ |
| 956 |
slang_type_specifier sp; |
| 957 |
slang_type_specifier_construct (&sp); |
| 958 |
if (!parse_type_specifier (C, &sp, spec->_struct->structs, scope, funcs)) |
| 959 |
{ |
| 960 |
slang_type_specifier_destruct (&sp); |
| 961 |
return 0; |
| 962 |
} |
| 963 |
do |
| 964 |
{ |
| 965 |
slang_variable *var; |
| 966 |
spec->_struct->fields->variables = (slang_variable *) slang_alloc_realloc ( |
| 967 |
spec->_struct->fields->variables, |
| 968 |
spec->_struct->fields->num_variables * sizeof (slang_variable), |
| 969 |
(spec->_struct->fields->num_variables + 1) * sizeof (slang_variable)); |
| 970 |
if (spec->_struct->fields->variables == NULL) |
| 971 |
{ |
| 972 |
slang_type_specifier_destruct (&sp); |
| 973 |
slang_info_log_memory (C->L); |
| 974 |
return 0; |
| 975 |
} |
| 976 |
var = spec->_struct->fields->variables + spec->_struct->fields->num_variables; |
| 977 |
spec->_struct->fields->num_variables++; |
| 978 |
slang_variable_construct (var); |
| 979 |
if (!slang_type_specifier_copy (&var->type.specifier, &sp)) |
| 980 |
{ |
| 981 |
slang_type_specifier_destruct (&sp); |
| 982 |
return 0; |
| 983 |
} |
| 984 |
if (!parse_identifier (C, &var->name)) |
| 985 |
{ |
| 986 |
slang_type_specifier_destruct (&sp); |
| 987 |
return 0; |
| 988 |
} |
| 989 |
switch (*C->I++) |
| 990 |
{ |
| 991 |
case FIELD_NONE: |
| 992 |
break; |
| 993 |
case FIELD_ARRAY: |
| 994 |
var->array_size = (slang_operation *) slang_alloc_malloc (sizeof ( |
| 995 |
slang_operation)); |
| 996 |
if (var->array_size == NULL) |
| 997 |
{ |
| 998 |
slang_type_specifier_destruct (&sp); |
| 999 |
slang_info_log_memory (C->L); |
| 1000 |
return 0; |
| 1001 |
} |
| 1002 |
if (!slang_operation_construct_a (var->array_size)) |
| 1003 |
{ |
| 1004 |
slang_alloc_free (var->array_size); |
| 1005 |
var->array_size = NULL; |
| 1006 |
slang_type_specifier_destruct (&sp); |
| 1007 |
slang_info_log_memory (C->L); |
| 1008 |
return 0; |
| 1009 |
} |
| 1010 |
if (!parse_expression (C, var->array_size, scope, structs, funcs)) |
| 1011 |
{ |
| 1012 |
slang_type_specifier_destruct (&sp); |
| 1013 |
return 0; |
| 1014 |
} |
| 1015 |
break; |
| 1016 |
default: |
| 1017 |
return 0; |
| 1018 |
} |
| 1019 |
} |
| 1020 |
while (*C->I++ != FIELD_NONE); |
| 1021 |
} |
| 1022 |
while (*C->I++ != FIELD_NONE); |
| 1023 |
if (*spec->_struct->name != '\0') |
| 1024 |
{ |
| 1025 |
slang_struct *s; |
| 1026 |
structs->structs = (slang_struct *) slang_alloc_realloc (structs->structs, |
| 1027 |
structs->num_structs * sizeof (slang_struct), |
| 1028 |
(structs->num_structs + 1) * sizeof (slang_struct)); |
| 1029 |
if (structs->structs == NULL) |
| 1030 |
{ |
| 1031 |
slang_info_log_memory (C->L); |
| 1032 |
return 0; |
| 1033 |
} |
| 1034 |
s = structs->structs + structs->num_structs; |
| 1035 |
if (!slang_struct_construct_a (s)) |
| 1036 |
return 0; |
| 1037 |
structs->num_structs++; |
| 1038 |
if (!slang_struct_copy (s, spec->_struct)) |
| 1039 |
return 0; |
| 1040 |
} |
| 1041 |
break; |
| 1042 |
case TYPE_SPECIFIER_TYPENAME: |
| 1043 |
spec->type = slang_spec_struct; |
| 1044 |
{ |
| 1045 |
char *name; |
| 1046 |
slang_struct *stru; |
| 1047 |
if (!parse_identifier (C, &name)) |
| 1048 |
return 0; |
| 1049 |
stru = slang_struct_scope_find (structs, name, 1); |
| 1050 |
if (stru == NULL) |
| 1051 |
{ |
| 1052 |
slang_info_log_error (C->L, "%s: undeclared type name", name); |
| 1053 |
slang_alloc_free (name); |
| 1054 |
return 0; |
| 1055 |
} |
| 1056 |
slang_alloc_free (name); |
| 1057 |
spec->_struct = (slang_struct *) slang_alloc_malloc (sizeof (slang_struct)); |
| 1058 |
if (spec->_struct == NULL) |
| 1059 |
{ |
| 1060 |
slang_info_log_memory (C->L); |
| 1061 |
return 0; |
| 1062 |
} |
| 1063 |
if (!slang_struct_construct_a (spec->_struct)) |
| 1064 |
{ |
| 1065 |
slang_alloc_free (spec->_struct); |
| 1066 |
spec->_struct = NULL; |
| 1067 |
return 0; |
| 1068 |
} |
| 1069 |
if (!slang_struct_copy (spec->_struct, stru)) |
| 1070 |
return 0; |
| 1071 |
} |
| 1072 |
break; |
| 1073 |
default: |
| 1074 |
return 0; |
| 1075 |
} |
| 1076 |
return 1; |
| 1077 |
} |
| 1078 |
|
| 1079 |
static int parse_fully_specified_type (slang_parse_ctx *C, slang_fully_specified_type *type, |
| 1080 |
slang_struct_scope *structs, slang_variable_scope *scope, slang_function_scope *funcs) |
| 1081 |
{ |
| 1082 |
if (!parse_type_qualifier (C, &type->qualifier)) |
| 1083 |
return 0; |
| 1084 |
return parse_type_specifier (C, &type->specifier, structs, scope, funcs); |
| 1085 |
} |
| 1086 |
|
| 1087 |
/* operation */ |
| 1088 |
#define OP_END 0 |
| 1089 |
#define OP_BLOCK_BEGIN_NO_NEW_SCOPE 1 |
| 1090 |
#define OP_BLOCK_BEGIN_NEW_SCOPE 2 |
| 1091 |
#define OP_DECLARE 3 |
| 1092 |
#define OP_ASM 4 |
| 1093 |
#define OP_BREAK 5 |
| 1094 |
#define OP_CONTINUE 6 |
| 1095 |
#define OP_DISCARD 7 |
| 1096 |
#define OP_RETURN 8 |
| 1097 |
#define OP_EXPRESSION 9 |
| 1098 |
#define OP_IF 10 |
| 1099 |
#define OP_WHILE 11 |
| 1100 |
#define OP_DO 12 |
| 1101 |
#define OP_FOR 13 |
| 1102 |
#define OP_PUSH_VOID 14 |
| 1103 |
#define OP_PUSH_BOOL 15 |
| 1104 |
#define OP_PUSH_INT 16 |
| 1105 |
#define OP_PUSH_FLOAT 17 |
| 1106 |
#define OP_PUSH_IDENTIFIER 18 |
| 1107 |
#define OP_SEQUENCE 19 |
| 1108 |
#define OP_ASSIGN 20 |
| 1109 |
#define OP_ADDASSIGN 21 |
| 1110 |
#define OP_SUBASSIGN 22 |
| 1111 |
#define OP_MULASSIGN 23 |
| 1112 |
#define OP_DIVASSIGN 24 |
| 1113 |
/*#define OP_MODASSIGN 25*/ |
| 1114 |
/*#define OP_LSHASSIGN 26*/ |
| 1115 |
/*#define OP_RSHASSIGN 27*/ |
| 1116 |
/*#define OP_ORASSIGN 28*/ |
| 1117 |
/*#define OP_XORASSIGN 29*/ |
| 1118 |
/*#define OP_ANDASSIGN 30*/ |
| 1119 |
#define OP_SELECT 31 |
| 1120 |
#define OP_LOGICALOR 32 |
| 1121 |
#define OP_LOGICALXOR 33 |
| 1122 |
#define OP_LOGICALAND 34 |
| 1123 |
/*#define OP_BITOR 35*/ |
| 1124 |
/*#define OP_BITXOR 36*/ |
| 1125 |
/*#define OP_BITAND 37*/ |
| 1126 |
#define OP_EQUAL 38 |
| 1127 |
#define OP_NOTEQUAL 39 |
| 1128 |
#define OP_LESS 40 |
| 1129 |
#define OP_GREATER 41 |
| 1130 |
#define OP_LESSEQUAL 42 |
| 1131 |
#define OP_GREATEREQUAL 43 |
| 1132 |
/*#define OP_LSHIFT 44*/ |
| 1133 |
/*#define OP_RSHIFT 45*/ |
| 1134 |
#define OP_ADD 46 |
| 1135 |
#define OP_SUBTRACT 47 |
| 1136 |
#define OP_MULTIPLY 48 |
| 1137 |
#define OP_DIVIDE 49 |
| 1138 |
/*#define OP_MODULUS 50*/ |
| 1139 |
#define OP_PREINCREMENT 51 |
| 1140 |
#define OP_PREDECREMENT 52 |
| 1141 |
#define OP_PLUS 53 |
| 1142 |
#define OP_MINUS 54 |
| 1143 |
/*#define OP_COMPLEMENT 55*/ |
| 1144 |
#define OP_NOT 56 |
| 1145 |
#define OP_SUBSCRIPT 57 |
| 1146 |
#define OP_CALL 58 |
| 1147 |
#define OP_FIELD 59 |
| 1148 |
#define OP_POSTINCREMENT 60 |
| 1149 |
#define OP_POSTDECREMENT 61 |
| 1150 |
|
| 1151 |
static int parse_child_operation (slang_parse_ctx *C, slang_operation *oper, int statement, |
| 1152 |
slang_variable_scope *scope, slang_struct_scope *structs, slang_function_scope *funcs) |
| 1153 |
{ |
| 1154 |
oper->children = (slang_operation *) slang_alloc_realloc (oper->children, |
| 1155 |
oper->num_children * sizeof (slang_operation), |
| 1156 |
(oper->num_children + 1) * sizeof (slang_operation)); |
| 1157 |
if (oper->children == NULL) |
| 1158 |
{ |
| 1159 |
slang_info_log_memory (C->L); |
| 1160 |
return 0; |
| 1161 |
} |
| 1162 |
if (!slang_operation_construct_a (oper->children + oper->num_children)) |
| 1163 |
{ |
| 1164 |
slang_info_log_memory (C->L); |
| 1165 |
return 0; |
| 1166 |
} |
| 1167 |
oper->num_children++; |
| 1168 |
if (statement) |
| 1169 |
return parse_statement (C, oper->children + oper->num_children - 1, scope, structs, funcs); |
| 1170 |
return parse_expression (C, oper->children + oper->num_children - 1, scope, structs, funcs); |
| 1171 |
} |
| 1172 |
|
| 1173 |
static int parse_declaration (slang_parse_ctx *C, slang_variable_scope *, slang_struct_scope *, |
| 1174 |
slang_function_scope *); |
| 1175 |
|
| 1176 |
static int parse_statement (slang_parse_ctx *C, slang_operation *oper, slang_variable_scope *scope, |
| 1177 |
slang_struct_scope *structs, slang_function_scope *funcs) |
| 1178 |
{ |
| 1179 |
oper->locals->outer_scope = scope; |
| 1180 |
switch (*C->I++) |
| 1181 |
{ |
| 1182 |
case OP_BLOCK_BEGIN_NO_NEW_SCOPE: |
| 1183 |
oper->type = slang_oper_block_no_new_scope; |
| 1184 |
while (*C->I != OP_END) |
| 1185 |
if (!parse_child_operation (C, oper, 1, scope, structs, funcs)) |
| 1186 |
return 0; |
| 1187 |
C->I++; |
| 1188 |
break; |
| 1189 |
case OP_BLOCK_BEGIN_NEW_SCOPE: |
| 1190 |
oper->type = slang_oper_block_new_scope; |
| 1191 |
while (*C->I != OP_END) |
| 1192 |
if (!parse_child_operation (C, oper, 1, oper->locals, structs, funcs)) |
| 1193 |
return 0; |
| 1194 |
C->I++; |
| 1195 |
break; |
| 1196 |
case OP_DECLARE: |
| 1197 |
oper->type = slang_oper_variable_decl; |
| 1198 |
{ |
| 1199 |
const unsigned int first_var = scope->num_variables; |
| 1200 |
if (!parse_declaration (C, scope, structs, funcs)) |
| 1201 |
return 0; |
| 1202 |
if (first_var < scope->num_variables) |
| 1203 |
{ |
| 1204 |
const unsigned int num_vars = scope->num_variables - first_var; |
| 1205 |
unsigned int i; |
| 1206 |
oper->children = (slang_operation *) slang_alloc_malloc (num_vars * sizeof ( |
| 1207 |
slang_operation)); |
| 1208 |
if (oper->children == NULL) |
| 1209 |
{ |
| 1210 |
slang_info_log_memory (C->L); |
| 1211 |
return 0; |
| 1212 |
} |
| 1213 |
for (i = 0; i < num_vars; i++) |
| 1214 |
if (!slang_operation_construct_a (oper->children + i)) |
| 1215 |
{ |
| 1216 |
unsigned int j; |
| 1217 |
for (j = 0; j < i; j++) |
| 1218 |
slang_operation_destruct (oper->children + j); |
| 1219 |
slang_alloc_free (oper->children); |
| 1220 |
oper->children = NULL; |
| 1221 |
slang_info_log_memory (C->L); |
| 1222 |
return 0; |
| 1223 |
} |
| 1224 |
oper->num_children = num_vars; |
| 1225 |
for (i = first_var; i < scope->num_variables; i++) |
| 1226 |
{ |
| 1227 |
slang_operation *o = oper->children + i - first_var; |
| 1228 |
o->type = slang_oper_identifier; |
| 1229 |
o->locals->outer_scope = scope; |
| 1230 |
o->identifier = slang_string_duplicate (scope->variables[i].name); |
| 1231 |
if (o->identifier == NULL) |
| 1232 |
{ |
| 1233 |
slang_info_log_memory (C->L); |
| 1234 |
return 0; |
| 1235 |
} |
| 1236 |
} |
| 1237 |
} |
| 1238 |
} |
| 1239 |
break; |
| 1240 |
case OP_ASM: |
| 1241 |
oper->type = slang_oper_asm; |
| 1242 |
if (!parse_identifier (C, &oper->identifier)) |
| 1243 |
return 0; |
| 1244 |
while (*C->I != OP_END) |
| 1245 |
if (!parse_child_operation (C, oper, 0, scope, structs, funcs)) |
| 1246 |
return 0; |
| 1247 |
C->I++; |
| 1248 |
break; |
| 1249 |
case OP_BREAK: |
| 1250 |
oper->type = slang_oper_break; |
| 1251 |
break; |
| 1252 |
case OP_CONTINUE: |
| 1253 |
oper->type = slang_oper_continue; |
| 1254 |
break; |
| 1255 |
case OP_DISCARD: |
| 1256 |
oper->type = slang_oper_discard; |
| 1257 |
break; |
| 1258 |
case OP_RETURN: |
| 1259 |
oper->type = slang_oper_return; |
| 1260 |
if (!parse_child_operation (C, oper, 0, scope, structs, funcs)) |
| 1261 |
return 0; |
| 1262 |
break; |
| 1263 |
case OP_EXPRESSION: |
| 1264 |
oper->type = slang_oper_expression; |
| 1265 |
if (!parse_child_operation (C, oper, 0, scope, structs, funcs)) |
| 1266 |
return 0; |
| 1267 |
break; |
| 1268 |
case OP_IF: |
| 1269 |
oper->type = slang_oper_if; |
| 1270 |
if (!parse_child_operation (C, oper, 0, scope, structs, funcs)) |
| 1271 |
return 0; |
| 1272 |
if (!parse_child_operation (C, oper, 1, scope, structs, funcs)) |
| 1273 |
return 0; |
| 1274 |
if (!parse_child_operation (C, oper, 1, scope, structs, funcs)) |
| 1275 |
return 0; |
| 1276 |
break; |
| 1277 |
case OP_WHILE: |
| 1278 |
oper->type = slang_oper_while; |
| 1279 |
if (!parse_child_operation (C, oper, 1, oper->locals, structs, funcs)) |
| 1280 |
return 0; |
| 1281 |
if (!parse_child_operation (C, oper, 1, oper->locals, structs, funcs)) |
| 1282 |
return 0; |
| 1283 |
break; |
| 1284 |
case OP_DO: |
| 1285 |
oper->type = slang_oper_do; |
| 1286 |
if (!parse_child_operation (C, oper, 1, scope, structs, funcs)) |
| 1287 |
return 0; |
| 1288 |
if (!parse_child_operation (C, oper, 0, scope, structs, funcs)) |
| 1289 |
return 0; |
| 1290 |
break; |
| 1291 |
case OP_FOR: |
| 1292 |
oper->type = slang_oper_for; |
| 1293 |
if (!parse_child_operation (C, oper, 1, oper->locals, structs, funcs)) |
| 1294 |
return 0; |
| 1295 |
if (!parse_child_operation (C, oper, 1, oper->locals, structs, funcs)) |
| 1296 |
return 0; |
| 1297 |
if (!parse_child_operation (C, oper, 0, oper->locals, structs, funcs)) |
| 1298 |
return 0; |
| 1299 |
if (!parse_child_operation (C, oper, 1, oper->locals, structs, funcs)) |
| 1300 |
return 0; |
| 1301 |
break; |
| 1302 |
default: |
| 1303 |
return 0; |
| 1304 |
} |
| 1305 |
return 1; |
| 1306 |
} |
| 1307 |
|
| 1308 |
static int handle_trinary_expression (slang_parse_ctx *C, slang_operation *op, |
| 1309 |
slang_operation **ops, unsigned int *num_ops) |
| 1310 |
{ |
| 1311 |
op->num_children = 3; |
| 1312 |
op->children = (slang_operation *) slang_alloc_malloc (3 * sizeof (slang_operation)); |
| 1313 |
if (op->children == NULL) |
| 1314 |
{ |
| 1315 |
slang_info_log_memory (C->L); |
| 1316 |
return 0; |
| 1317 |
} |
| 1318 |
op->children[0] = (*ops)[*num_ops - 4]; |
| 1319 |
op->children[1] = (*ops)[*num_ops - 3]; |
| 1320 |
op->children[2] = (*ops)[*num_ops - 2]; |
| 1321 |
(*ops)[*num_ops - 4] = (*ops)[*num_ops - 1]; |
| 1322 |
*num_ops -= 3; |
| 1323 |
*ops = (slang_operation *) slang_alloc_realloc (*ops, (*num_ops + 3) * sizeof (slang_operation), |
| 1324 |
*num_ops * sizeof (slang_operation)); |
| 1325 |
if (*ops == NULL) |
| 1326 |
{ |
| 1327 |
slang_info_log_memory (C->L); |
| 1328 |
return 0; |
| 1329 |
} |
| 1330 |
return 1; |
| 1331 |
} |
| 1332 |
|
| 1333 |
static int handle_binary_expression (slang_parse_ctx *C, slang_operation *op, |
| 1334 |
slang_operation **ops, unsigned int *num_ops) |
| 1335 |
{ |
| 1336 |
op->num_children = 2; |
| 1337 |
op->children = (slang_operation *) slang_alloc_malloc (2 * sizeof (slang_operation)); |
| 1338 |
if (op->children == NULL) |
| 1339 |
{ |
| 1340 |
slang_info_log_memory (C->L); |
| 1341 |
return 0; |
| 1342 |
} |
| 1343 |
op->children[0] = (*ops)[*num_ops - 3]; |
| 1344 |
op->children[1] = (*ops)[*num_ops - 2]; |
| 1345 |
(*ops)[*num_ops - 3] = (*ops)[*num_ops - 1]; |
| 1346 |
*num_ops -= 2; |
| 1347 |
*ops = (slang_operation *) slang_alloc_realloc (*ops, (*num_ops + 2) * sizeof (slang_operation), |
| 1348 |
*num_ops * sizeof (slang_operation)); |
| 1349 |
if (*ops == NULL) |
| 1350 |
{ |
| 1351 |
slang_info_log_memory (C->L); |
| 1352 |
return 0; |
| 1353 |
} |
| 1354 |
return 1; |
| 1355 |
} |
| 1356 |
|
| 1357 |
static int handle_unary_expression (slang_parse_ctx *C, slang_operation *op, |
| 1358 |
slang_operation **ops, unsigned int *num_ops) |
| 1359 |
{ |
| 1360 |
op->num_children = 1; |
| 1361 |
op->children = (slang_operation *) slang_alloc_malloc (sizeof (slang_operation)); |
| 1362 |
if (op->children == NULL) |
| 1363 |
{ |
| 1364 |
slang_info_log_memory (C->L); |
| 1365 |
return 0; |
| 1366 |
} |
| 1367 |
op->children[0] = (*ops)[*num_ops - 2]; |
| 1368 |
(*ops)[*num_ops - 2] = (*ops)[*num_ops - 1]; |
| 1369 |
(*num_ops)--; |
| 1370 |
*ops = (slang_operation *) slang_alloc_realloc (*ops, (*num_ops + 1) * sizeof (slang_operation), |
| 1371 |
*num_ops * sizeof (slang_operation)); |
| 1372 |
if (*ops == NULL) |
| 1373 |
{ |
| 1374 |
slang_info_log_memory (C->L); |
| 1375 |
return 0; |
| 1376 |
} |
| 1377 |
return 1; |
| 1378 |
} |
| 1379 |
|
| 1380 |
static int is_constructor_name (const char *name, slang_struct_scope *structs) |
| 1381 |
{ |
| 1382 |
if (slang_type_specifier_type_from_string (name) != slang_spec_void) |
| 1383 |
return 1; |
| 1384 |
return slang_struct_scope_find (structs, name, 1) != NULL; |
| 1385 |
} |
| 1386 |
|
| 1387 |
static int parse_expression (slang_parse_ctx *C, slang_operation *oper, slang_variable_scope *scope, |
| 1388 |
slang_struct_scope *structs, slang_function_scope *funcs) |
| 1389 |
{ |
| 1390 |
slang_operation *ops = NULL; |
| 1391 |
unsigned int num_ops = 0; |
| 1392 |
int number; |
| 1393 |
|
| 1394 |
while (*C->I != OP_END) |
| 1395 |
{ |
| 1396 |
slang_operation *op; |
| 1397 |
const unsigned int op_code = *C->I++; |
| 1398 |
ops = (slang_operation *) slang_alloc_realloc (ops, |
| 1399 |
num_ops * sizeof (slang_operation), (num_ops + 1) * sizeof (slang_operation)); |
| 1400 |
if (ops == NULL) |
| 1401 |
{ |
| 1402 |
slang_info_log_memory (C->L); |
| 1403 |
return 0; |
| 1404 |
} |
| 1405 |
op = ops + num_ops; |
| 1406 |
if (!slang_operation_construct_a (op)) |
| 1407 |
{ |
| 1408 |
slang_info_log_memory (C->L); |
| 1409 |
return 0; |
| 1410 |
} |
| 1411 |
num_ops++; |
| 1412 |
op->locals->outer_scope = scope; |
| 1413 |
switch (op_code) |
| 1414 |
{ |
| 1415 |
case OP_PUSH_VOID: |
| 1416 |
op->type = slang_oper_void; |
| 1417 |
break; |
| 1418 |
case OP_PUSH_BOOL: |
| 1419 |
op->type = slang_oper_literal_bool; |
| 1420 |
if (!parse_number (C, &number)) |
| 1421 |
return 0; |
| 1422 |
op->literal = (float) number; |
| 1423 |
break; |
| 1424 |
case OP_PUSH_INT: |
| 1425 |
op->type = slang_oper_literal_int; |
| 1426 |
if (!parse_number (C, &number)) |
| 1427 |
return 0; |
| 1428 |
op->literal = (float) number; |
| 1429 |
break; |
| 1430 |
case OP_PUSH_FLOAT: |
| 1431 |
op->type = slang_oper_literal_float; |
| 1432 |
if (!parse_float (C, &op->literal)) |
| 1433 |
return 0; |
| 1434 |
break; |
| 1435 |
case OP_PUSH_IDENTIFIER: |
| 1436 |
op->type = slang_oper_identifier; |
| 1437 |
if (!parse_identifier (C, &op->identifier)) |
| 1438 |
return 0; |
| 1439 |
break; |
| 1440 |
case OP_SEQUENCE: |
| 1441 |
op->type = slang_oper_sequence; |
| 1442 |
if (!handle_binary_expression (C, op, &ops, &num_ops)) |
| 1443 |
return 0; |
| 1444 |
break; |
| 1445 |
case OP_ASSIGN: |
| 1446 |
op->type = slang_oper_assign; |
| 1447 |
if (!handle_binary_expression (C, op, &ops, &num_ops)) |
| 1448 |
return 0; |
| 1449 |
break; |
| 1450 |
case OP_ADDASSIGN: |
| 1451 |
op->type = slang_oper_addassign; |
| 1452 |
if (!handle_binary_expression (C, op, &ops, &num_ops)) |
| 1453 |
return 0; |
| 1454 |
break; |
| 1455 |
case OP_SUBASSIGN: |
| 1456 |
op->type = slang_oper_subassign; |
| 1457 |
if (!handle_binary_expression (C, op, &ops, &num_ops)) |
| 1458 |
return 0; |
| 1459 |
break; |
| 1460 |
case OP_MULASSIGN: |
| 1461 |
op->type = slang_oper_mulassign; |
| 1462 |
if (!handle_binary_expression (C, op, &ops, &num_ops)) |
| 1463 |
return 0; |
| 1464 |
break; |
| 1465 |
case OP_DIVASSIGN: |
| 1466 |
op->type = slang_oper_divassign; |
| 1467 |
if (!handle_binary_expression (C, op, &ops, &num_ops)) |
| 1468 |
return 0; |
| 1469 |
break; |
| 1470 |
/*case OP_MODASSIGN:*/ |
| 1471 |
/*case OP_LSHASSIGN:*/ |
| 1472 |
/*case OP_RSHASSIGN:*/ |
| 1473 |
/*case OP_ORASSIGN:*/ |
| 1474 |
/*case OP_XORASSIGN:*/ |
| 1475 |
/*case OP_ANDASSIGN:*/ |
| 1476 |
case OP_SELECT: |
| 1477 |
op->type = slang_oper_select; |
| 1478 |
if (!handle_trinary_expression (C, op, &ops, &num_ops)) |
| 1479 |
return 0; |
| 1480 |
break; |
| 1481 |
case OP_LOGICALOR: |
| 1482 |
op->type = slang_oper_logicalor; |
| 1483 |
if (!handle_binary_expression (C, op, &ops, &num_ops)) |
| 1484 |
return 0; |
| 1485 |
break; |
| 1486 |
case OP_LOGICALXOR: |
| 1487 |
op->type = slang_oper_logicalxor; |
| 1488 |
if (!handle_binary_expression (C, op, &ops, &num_ops)) |
| 1489 |
return 0; |
| 1490 |
break; |
| 1491 |
case OP_LOGICALAND: |
| 1492 |
op->type = slang_oper_logicaland; |
| 1493 |
if (!handle_binary_expression (C, op, &ops, &num_ops)) |
| 1494 |
return 0; |
| 1495 |
break; |
| 1496 |
/*case OP_BITOR:*/ |
| 1497 |
/*case OP_BITXOR:*/ |
| 1498 |
/*case OP_BITAND:*/ |
| 1499 |
case OP_EQUAL: |
| 1500 |
op->type = slang_oper_equal; |
| 1501 |
if (!handle_binary_expression (C, op, &ops, &num_ops)) |
| 1502 |
return 0; |
| 1503 |
break; |
| 1504 |
case OP_NOTEQUAL: |
| 1505 |
op->type = slang_oper_notequal; |
| 1506 |
if (!handle_binary_expression (C, op, &ops, &num_ops)) |
| 1507 |
return 0; |
| 1508 |
break; |
| 1509 |
case OP_LESS: |
| 1510 |
op->type = slang_oper_less; |
| 1511 |
if (!handle_binary_expression (C, op, &ops, &num_ops)) |
| 1512 |
return 0; |
| 1513 |
break; |
| 1514 |
case OP_GREATER: |
| 1515 |
op->type = slang_oper_greater; |
| 1516 |
if (!handle_binary_expression (C, op, &ops, &num_ops)) |
| 1517 |
return 0; |
| 1518 |
break; |
| 1519 |
case OP_LESSEQUAL: |
| 1520 |
op->type = slang_oper_lessequal; |
| 1521 |
if (!handle_binary_expression (C, op, &ops, &num_ops)) |
| 1522 |
return 0; |
| 1523 |
break; |
| 1524 |
case OP_GREATEREQUAL: |
| 1525 |
op->type = slang_oper_greaterequal; |
| 1526 |
if (!handle_binary_expression (C, op, &ops, &num_ops)) |
| 1527 |
return 0; |
| 1528 |
break; |
| 1529 |
/*case OP_LSHIFT:*/ |
| 1530 |
/*case OP_RSHIFT:*/ |
| 1531 |
case OP_ADD: |
| 1532 |
op->type = slang_oper_add; |
| 1533 |
if (!handle_binary_expression (C, op, &ops, &num_ops)) |
| 1534 |
return 0; |
| 1535 |
break; |
| 1536 |
case OP_SUBTRACT: |
| 1537 |
op->type = slang_oper_subtract; |
| 1538 |
if (!handle_binary_expression (C, op, &ops, &num_ops)) |
| 1539 |
return 0; |
| 1540 |
break; |
| 1541 |
case OP_MULTIPLY: |
| 1542 |
op->type = slang_oper_multiply; |
| 1543 |
if (!handle_binary_expression (C, op, &ops, &num_ops)) |
| 1544 |
return 0; |
| 1545 |
break; |
| 1546 |
case OP_DIVIDE: |
| 1547 |
op->type = slang_oper_divide; |
| 1548 |
if (!handle_binary_expression (C, op, &ops, &num_ops)) |
| 1549 |
return 0; |
| 1550 |
break; |
| 1551 |
/*case OP_MODULUS:*/ |
| 1552 |
case OP_PREINCREMENT: |
| 1553 |
op->type = slang_oper_preincrement; |
| 1554 |
if (!handle_unary_expression (C, op, &ops, &num_ops)) |
| 1555 |
return 0; |
| 1556 |
break; |
| 1557 |
case OP_PREDECREMENT: |
| 1558 |
op->type = slang_oper_predecrement; |
| 1559 |
if (!handle_unary_expression (C, op, &ops, &num_ops)) |
| 1560 |
return 0; |
| 1561 |
break; |
| 1562 |
case OP_PLUS: |
| 1563 |
op->type = slang_oper_plus; |
| 1564 |
if (!handle_unary_expression (C, op, &ops, &num_ops)) |
| 1565 |
return 0; |
| 1566 |
break; |
| 1567 |
case OP_MINUS: |
| 1568 |
op->type = slang_oper_minus; |
| 1569 |
if (!handle_unary_expression (C, op, &ops, &num_ops)) |
| 1570 |
return 0; |
| 1571 |
break; |
| 1572 |
case OP_NOT: |
| 1573 |
op->type = slang_oper_not; |
| 1574 |
if (!handle_unary_expression (C, op, &ops, &num_ops)) |
| 1575 |
return 0; |
| 1576 |
break; |
| 1577 |
/*case OP_COMPLEMENT:*/ |
| 1578 |
case OP_SUBSCRIPT: |
| 1579 |
op->type = slang_oper_subscript; |
| 1580 |
if (!handle_binary_expression (C, op, &ops, &num_ops)) |
| 1581 |
return 0; |
| 1582 |
break; |
| 1583 |
case OP_CALL: |
| 1584 |
op->type = slang_oper_call; |
| 1585 |
if (!parse_identifier (C, &op->identifier)) |
| 1586 |
return 0; |
| 1587 |
while (*C->I != OP_END) |
| 1588 |
if (!parse_child_operation (C, op, 0, scope, structs, funcs)) |
| 1589 |
return 0; |
| 1590 |
C->I++; |
| 1591 |
if (!C->parsing_builtin && |
| 1592 |
!slang_function_scope_find_by_name (funcs, op->identifier, 1) && |
| 1593 |
!is_constructor_name (op->identifier, structs)) |
| 1594 |
{ |
| 1595 |
slang_info_log_error (C->L, "%s: undeclared function name", op->identifier); |
| 1596 |
return 0; |
| 1597 |
} |
| 1598 |
break; |
| 1599 |
case OP_FIELD: |
| 1600 |
op->type = slang_oper_field; |
| 1601 |
if (!parse_identifier (C, &op->identifier)) |
| 1602 |
return 0; |
| 1603 |
if (!handle_unary_expression (C, op, &ops, &num_ops)) |
| 1604 |
return 0; |
| 1605 |
break; |
| 1606 |
case OP_POSTINCREMENT: |
| 1607 |
op->type = slang_oper_postincrement; |
| 1608 |
if (!handle_unary_expression (C, op, &ops, &num_ops)) |
| 1609 |
return 0; |
| 1610 |
break; |
| 1611 |
case OP_POSTDECREMENT: |
| 1612 |
op->type = slang_oper_postdecrement; |
| 1613 |
if (!handle_unary_expression (C, op, &ops, &num_ops)) |
| 1614 |
return 0; |
| 1615 |
break; |
| 1616 |
default: |
| 1617 |
return 0; |
| 1618 |
} |
| 1619 |
} |
| 1620 |
C->I++; |
| 1621 |
*oper = *ops; |
| 1622 |
slang_alloc_free (ops); |
| 1623 |
return 1; |
| 1624 |
} |
| 1625 |
|
| 1626 |
/* parameter qualifier */ |
| 1627 |
#define PARAM_QUALIFIER_IN 0 |
| 1628 |
#define PARAM_QUALIFIER_OUT 1 |
| 1629 |
#define PARAM_QUALIFIER_INOUT 2 |
| 1630 |
|
| 1631 |
/* function parameter array presence */ |
| 1632 |
#define PARAMETER_ARRAY_NOT_PRESENT 0 |
| 1633 |
#define PARAMETER_ARRAY_PRESENT 1 |
| 1634 |
|
| 1635 |
static int parse_parameter_declaration (slang_parse_ctx *C, slang_variable *param, |
| 1636 |
slang_struct_scope *structs, slang_variable_scope *scope, slang_function_scope *funcs) |
| 1637 |
{ |
| 1638 |
slang_storage_aggregate agg; |
| 1639 |
if (!parse_type_qualifier (C, ¶m->type.qualifier)) |
| 1640 |
return 0; |
| 1641 |
switch (*C->I++) |
| 1642 |
{ |
| 1643 |
case PARAM_QUALIFIER_IN: |
| 1644 |
if (param->type.qualifier != slang_qual_const && param->type.qualifier != slang_qual_none) |
| 1645 |
{ |
| 1646 |
slang_info_log_error (C->L, "invalid type qualifier"); |
| 1647 |
return 0; |
| 1648 |
} |
| 1649 |
break; |
| 1650 |
case PARAM_QUALIFIER_OUT: |
| 1651 |
if (param->type.qualifier == slang_qual_none) |
| 1652 |
param->type.qualifier = slang_qual_out; |
| 1653 |
else |
| 1654 |
{ |
| 1655 |
slang_info_log_error (C->L, "invalid type qualifier"); |
| 1656 |
return 0; |
| 1657 |
} |
| 1658 |
break; |
| 1659 |
case PARAM_QUALIFIER_INOUT: |
| 1660 |
if (param->type.qualifier == slang_qual_none) |
| 1661 |
param->type.qualifier = slang_qual_inout; |
| 1662 |
else |
| 1663 |
{ |
| 1664 |
slang_info_log_error (C->L, "invalid type qualifier"); |
| 1665 |
return 0; |
| 1666 |
} |
| 1667 |
break; |
| 1668 |
default: |
| 1669 |
return 0; |
| 1670 |
} |
| 1671 |
if (!parse_type_specifier (C, ¶m->type.specifier, structs, scope, funcs)) |
| 1672 |
return 0; |
| 1673 |
if (!parse_identifier (C, ¶m->name)) |
| 1674 |
return 0; |
| 1675 |
if (*C->I++ == PARAMETER_ARRAY_PRESENT) |
| 1676 |
{ |
| 1677 |
param->array_size = (slang_operation *) slang_alloc_malloc (sizeof (slang_operation)); |
| 1678 |
if (param->array_size == NULL) |
| 1679 |
{ |
| 1680 |
slang_info_log_memory (C->L); |
| 1681 |
return 0; |
| 1682 |
} |
| 1683 |
if (!slang_operation_construct_a (param->array_size)) |
| 1684 |
{ |
| 1685 |
slang_alloc_free (param->array_size); |
| 1686 |
param->array_size = NULL; |
| 1687 |
slang_info_log_memory (C->L); |
| 1688 |
return 0; |
| 1689 |
} |
| 1690 |
if (!parse_expression (C, param->array_size, scope, structs, funcs)) |
| 1691 |
return 0; |
| 1692 |
} |
| 1693 |
slang_storage_aggregate_construct (&agg); |
| 1694 |
if (!_slang_aggregate_variable (&agg, ¶m->type.specifier, param->array_size, funcs, |
| 1695 |
structs)) |
| 1696 |
{ |
| 1697 |
slang_storage_aggregate_destruct (&agg); |
| 1698 |
return 0; |
| 1699 |
} |
| 1700 |
slang_storage_aggregate_destruct (&agg); |
| 1701 |
return 1; |
| 1702 |
} |
| 1703 |
|
| 1704 |
/* function type */ |
| 1705 |
#define FUNCTION_ORDINARY 0 |
| 1706 |
#define FUNCTION_CONSTRUCTOR 1 |
| 1707 |
#define FUNCTION_OPERATOR 2 |
| 1708 |
|
| 1709 |
/* function parameter */ |
| 1710 |
#define PARAMETER_NONE 0 |
| 1711 |
#define PARAMETER_NEXT 1 |
| 1712 |
|
| 1713 |
/* operator type */ |
| 1714 |
#define OPERATOR_ASSIGN 1 |
| 1715 |
#define OPERATOR_ADDASSIGN 2 |
| 1716 |
#define OPERATOR_SUBASSIGN 3 |
| 1717 |
#define OPERATOR_MULASSIGN 4 |
| 1718 |
#define OPERATOR_DIVASSIGN 5 |
| 1719 |
/*#define OPERATOR_MODASSIGN 6*/ |
| 1720 |
/*#define OPERATOR_LSHASSIGN 7*/ |
| 1721 |
/*#define OPERATOR_RSHASSIGN 8*/ |
| 1722 |
/*#define OPERATOR_ANDASSIGN 9*/ |
| 1723 |
/*#define OPERATOR_XORASSIGN 10*/ |
| 1724 |
/*#define OPERATOR_ORASSIGN 11*/ |
| 1725 |
#define OPERATOR_LOGICALXOR 12 |
| 1726 |
/*#define OPERATOR_BITOR 13*/ |
| 1727 |
/*#define OPERATOR_BITXOR 14*/ |
| 1728 |
/*#define OPERATOR_BITAND 15*/ |
| 1729 |
#define OPERATOR_EQUAL 16 |
| 1730 |
#define OPERATOR_NOTEQUAL 17 |
| 1731 |
#define OPERATOR_LESS 18 |
| 1732 |
#define OPERATOR_GREATER 19 |
| 1733 |
#define OPERATOR_LESSEQUAL 20 |
| 1734 |
#define OPERATOR_GREATEREQUAL 21 |
| 1735 |
/*#define OPERATOR_LSHIFT 22*/ |
| 1736 |
/*#define OPERATOR_RSHIFT 23*/ |
| 1737 |
#define OPERATOR_MULTIPLY 24 |
| 1738 |
#define OPERATOR_DIVIDE 25 |
| 1739 |
/*#define OPERATOR_MODULUS 26*/ |
| 1740 |
#define OPERATOR_INCREMENT 27 |
| 1741 |
#define OPERATOR_DECREMENT 28 |
| 1742 |
#define OPERATOR_PLUS 29 |
| 1743 |
#define OPERATOR_MINUS 30 |
| 1744 |
/*#define OPERATOR_COMPLEMENT 31*/ |
| 1745 |
#define OPERATOR_NOT 32 |
| 1746 |
|
| 1747 |
static const struct { |
| 1748 |
unsigned int o_code; |
| 1749 |
const char *o_name; |
| 1750 |
} operator_names[] = { |
| 1751 |
{ OPERATOR_INCREMENT, "++" }, |
| 1752 |
{ OPERATOR_ADDASSIGN, "+=" }, |
| 1753 |
{ OPERATOR_PLUS, "+" }, |
| 1754 |
{ OPERATOR_DECREMENT, "--" }, |
| 1755 |
{ OPERATOR_SUBASSIGN, "-=" }, |
| 1756 |
{ OPERATOR_MINUS, "-" }, |
| 1757 |
{ OPERATOR_NOTEQUAL, "!=" }, |
| 1758 |
{ OPERATOR_NOT, "!" }, |
| 1759 |
{ OPERATOR_MULASSIGN, "*=" }, |
| 1760 |
{ OPERATOR_MULTIPLY, "*" }, |
| 1761 |
{ OPERATOR_DIVASSIGN, "/=" }, |
| 1762 |
{ OPERATOR_DIVIDE, "/" }, |
| 1763 |
{ OPERATOR_LESSEQUAL, "<=" }, |
| 1764 |
/*{ OPERATOR_LSHASSIGN, "<<=" },*/ |
| 1765 |
/*{ OPERATOR_LSHIFT, "<<" },*/ |
| 1766 |
{ OPERATOR_LESS, "<" }, |
| 1767 |
{ OPERATOR_GREATEREQUAL, ">=" }, |
| 1768 |
/*{ OPERATOR_RSHASSIGN, ">>=" },*/ |
| 1769 |
/*{ OPERATOR_RSHIFT, ">>" },*/ |
| 1770 |
{ OPERATOR_GREATER, ">" }, |
| 1771 |
{ OPERATOR_EQUAL, "==" }, |
| 1772 |
{ OPERATOR_ASSIGN, "=" }, |
| 1773 |
/*{ OPERATOR_MODASSIGN, "%=" },*/ |
| 1774 |
/*{ OPERATOR_MODULUS, "%" },*/ |
| 1775 |
/*{ OPERATOR_ANDASSIGN, "&=" },*/ |
| 1776 |
/*{ OPERATOR_BITAND, "&" },*/ |
| 1777 |
/*{ OPERATOR_ORASSIGN, "|=" },*/ |
| 1778 |
/*{ OPERATOR_BITOR, "|" },*/ |
| 1779 |
/*{ OPERATOR_COMPLEMENT, "~" },*/ |
| 1780 |
/*{ OPERATOR_XORASSIGN, "^=" },*/ |
| 1781 |
{ OPERATOR_LOGICALXOR, "^^" }/*,*/ |
| 1782 |
/*{ OPERATOR_BITXOR, "^" }*/ |
| 1783 |
}; |
| 1784 |
|
| 1785 |
static int parse_operator_name (slang_parse_ctx *C, char **pname) |
| 1786 |
{ |
| 1787 |
unsigned int i; |
| 1788 |
for (i = 0; i < sizeof (operator_names) / sizeof (*operator_names); i++) |
| 1789 |
if (operator_names[i].o_code == (unsigned int) (*C->I)) |
| 1790 |
{ |
| 1791 |
*pname = slang_string_duplicate (operator_names[i].o_name); |
| 1792 |
if (*pname == NULL) |
| 1793 |
{ |
| 1794 |
slang_info_log_memory (C->L); |
| 1795 |
return 0; |
| 1796 |
} |
| 1797 |
C->I++; |
| 1798 |
return 1; |
| 1799 |
} |
| 1800 |
return 0; |
| 1801 |
} |
| 1802 |
|
| 1803 |
static int parse_function_prototype (slang_parse_ctx *C, slang_function *func, |
| 1804 |
slang_struct_scope *structs, slang_variable_scope *scope, slang_function_scope *funcs) |
| 1805 |
{ |
| 1806 |
if (!parse_fully_specified_type (C, &func->header.type, structs, scope, funcs)) |
| 1807 |
return 0; |
| 1808 |
switch (*C->I++) |
| 1809 |
{ |
| 1810 |
case FUNCTION_ORDINARY: |
| 1811 |
func->kind = slang_func_ordinary; |
| 1812 |
if (!parse_identifier (C, &func->header.name)) |
| 1813 |
return 0; |
| 1814 |
break; |
| 1815 |
case FUNCTION_CONSTRUCTOR: |
| 1816 |
func->kind = slang_func_constructor; |
| 1817 |
if (func->header.type.specifier.type == slang_spec_struct) |
| 1818 |
return 0; |
| 1819 |
func->header.name = slang_string_duplicate ( |
| 1820 |
type_specifier_type_names[func->header.type.specifier.type]); |
| 1821 |
if (func->header.name == NULL) |
| 1822 |
{ |
| 1823 |
slang_info_log_memory (C->L); |
| 1824 |
return 0; |
| 1825 |
} |
| 1826 |
break; |
| 1827 |
case FUNCTION_OPERATOR: |
| 1828 |
func->kind = slang_func_operator; |
| 1829 |
if (!parse_operator_name (C, &func->header.name)) |
| 1830 |
return 0; |
| 1831 |
break; |
| 1832 |
default: |
| 1833 |
return 0; |
| 1834 |
} |
| 1835 |
func->parameters->outer_scope = scope; |
| 1836 |
while (*C->I++ == PARAMETER_NEXT) |
| 1837 |
{ |
| 1838 |
func->parameters->variables = (slang_variable *) slang_alloc_realloc ( |
| 1839 |
func->parameters->variables, |
| 1840 |
func->parameters->num_variables * sizeof (slang_variable), |
| 1841 |
(func->parameters->num_variables + 1) * sizeof (slang_variable)); |
| 1842 |
if (func->parameters->variables == NULL) |
| 1843 |
{ |
| 1844 |
slang_info_log_memory (C->L); |
| 1845 |
return 0; |
| 1846 |
} |
| 1847 |
slang_variable_construct (func->parameters->variables + func->parameters->num_variables); |
| 1848 |
func->parameters->num_variables++; |
| 1849 |
if (!parse_parameter_declaration (C, func->parameters->variables + |
| 1850 |
func->parameters->num_variables - 1, structs, scope, funcs)) |
| 1851 |
return 0; |
| 1852 |
} |
| 1853 |
func->param_count = func->parameters->num_variables; |
| 1854 |
return 1; |
| 1855 |
} |
| 1856 |
|
| 1857 |
static int parse_function_definition (slang_parse_ctx *C, slang_function *func, |
| 1858 |
slang_struct_scope *structs, slang_variable_scope *scope, slang_function_scope *funcs) |
| 1859 |
{ |
| 1860 |
if (!parse_function_prototype (C, func, structs, scope, funcs)) |
| 1861 |
return 0; |
| 1862 |
func->body = (slang_operation *) slang_alloc_malloc (sizeof (slang_operation)); |
| 1863 |
if (func->body == NULL) |
| 1864 |
{ |
| 1865 |
slang_info_log_memory (C->L); |
| 1866 |
return 0; |
| 1867 |
} |
| 1868 |
if (!slang_operation_construct_a (func->body)) |
| 1869 |
{ |
| 1870 |
slang_alloc_free (func->body); |
| 1871 |
func->body = NULL; |
| 1872 |
slang_info_log_memory (C->L); |
| 1873 |
return 0; |
| 1874 |
} |
| 1875 |
if (!parse_statement (C, func->body, func->parameters, structs, funcs)) |
| 1876 |
return 0; |
| 1877 |
return 1; |
| 1878 |
} |
| 1879 |
|
| 1880 |
/* init declarator list */ |
| 1881 |
#define DECLARATOR_NONE 0 |
| 1882 |
#define DECLARATOR_NEXT 1 |
| 1883 |
|
| 1884 |
/* variable declaration */ |
| 1885 |
#define VARIABLE_NONE 0 |
| 1886 |
#define VARIABLE_IDENTIFIER 1 |
| 1887 |
#define VARIABLE_INITIALIZER 2 |
| 1888 |
#define VARIABLE_ARRAY_EXPLICIT 3 |
| 1889 |
#define VARIABLE_ARRAY_UNKNOWN 4 |
| 1890 |
|
| 1891 |
static int parse_init_declarator (slang_parse_ctx *C, const slang_fully_specified_type *type, |
| 1892 |
slang_variable_scope *vars, slang_struct_scope *structs, slang_function_scope *funcs) |
| 1893 |
{ |
| 1894 |
slang_variable *var; |
| 1895 |
|
| 1896 |
if (*C->I++ == VARIABLE_NONE) |
| 1897 |
return 1; |
| 1898 |
vars->variables = (slang_variable *) slang_alloc_realloc (vars->variables, |
| 1899 |
vars->num_variables * sizeof (slang_variable), |
| 1900 |
(vars->num_variables + 1) * sizeof (slang_variable)); |
| 1901 |
if (vars->variables == NULL) |
| 1902 |
{ |
| 1903 |
slang_info_log_memory (C->L); |
| 1904 |
return 0; |
| 1905 |
} |
| 1906 |
var = vars->variables + vars->num_variables; |
| 1907 |
vars->num_variables++; |
| 1908 |
slang_variable_construct (var); |
| 1909 |
var->type.qualifier = type->qualifier; |
| 1910 |
if (!parse_identifier (C, &var->name)) |
| 1911 |
return 0; |
| 1912 |
switch (*C->I++) |
| 1913 |
{ |
| 1914 |
case VARIABLE_NONE: |
| 1915 |
if (!slang_type_specifier_copy (&var->type.specifier, &type->specifier)) |
| 1916 |
return 0; |
| 1917 |
break; |
| 1918 |
case VARIABLE_INITIALIZER: |
| 1919 |
if (!slang_type_specifier_copy (&var->type.specifier, &type->specifier)) |
| 1920 |
return 0; |
| 1921 |
var->initializer = (slang_operation *) slang_alloc_malloc (sizeof (slang_operation)); |
| 1922 |
if (var->initializer == NULL) |
| 1923 |
{ |
| 1924 |
slang_info_log_memory (C->L); |
| 1925 |
return 0; |
| 1926 |
} |
| 1927 |
if (!slang_operation_construct_a (var->initializer)) |
| 1928 |
{ |
| 1929 |
slang_alloc_free (var->initializer); |
| 1930 |
var->initializer = NULL; |
| 1931 |
slang_info_log_memory (C->L); |
| 1932 |
return 0; |
| 1933 |
} |
| 1934 |
if (!parse_expression (C, var->initializer, vars, structs, funcs)) |
| 1935 |
return 0; |
| 1936 |
break; |
| 1937 |
case VARIABLE_ARRAY_UNKNOWN: |
| 1938 |
var->type.specifier.type = slang_spec_array; |
| 1939 |
var->type.specifier._array = (slang_type_specifier *) slang_alloc_malloc (sizeof ( |
| 1940 |
slang_type_specifier)); |
| 1941 |
if (var->type.specifier._array == NULL) |
| 1942 |
{ |
| 1943 |
slang_info_log_memory (C->L); |
| 1944 |
return 0; |
| 1945 |
} |
| 1946 |
slang_type_specifier_construct (var->type.specifier._array); |
| 1947 |
if (!slang_type_specifier_copy (var->type.specifier._array, &type->specifier)) |
| 1948 |
return 0; |
| 1949 |
break; |
| 1950 |
case VARIABLE_ARRAY_EXPLICIT: |
| 1951 |
var->type.specifier.type = slang_spec_array; |
| 1952 |
var->type.specifier._array = (slang_type_specifier *) slang_alloc_malloc (sizeof ( |
| 1953 |
slang_type_specifier)); |
| 1954 |
if (var->type.specifier._array == NULL) |
| 1955 |
{ |
| 1956 |
slang_info_log_memory (C->L); |
| 1957 |
return 0; |
| 1958 |
} |
| 1959 |
slang_type_specifier_construct (var->type.specifier._array); |
| 1960 |
if (!slang_type_specifier_copy (var->type.specifier._array, &type->specifier)) |
| 1961 |
return 0; |
| 1962 |
var->array_size = (slang_operation *) slang_alloc_malloc (sizeof (slang_operation)); |
| 1963 |
if (var->array_size == NULL) |
| 1964 |
{ |
| 1965 |
slang_info_log_memory (C->L); |
| 1966 |
return 0; |
| 1967 |
} |
| 1968 |
if (!slang_operation_construct_a (var->array_size)) |
| 1969 |
{ |
| 1970 |
slang_alloc_free (var->array_size); |
| 1971 |
var->array_size = NULL; |
| 1972 |
slang_info_log_memory (C->L); |
| 1973 |
return 0; |
| 1974 |
} |
| 1975 |
if (!parse_expression (C, var->array_size, vars, structs, funcs)) |
| 1976 |
return 0; |
| 1977 |
break; |
| 1978 |
default: |
| 1979 |
return 0; |
| 1980 |
} |
| 1981 |
if (!(var->type.specifier.type == slang_spec_array && var->array_size == NULL)) |
| 1982 |
{ |
| 1983 |
slang_storage_aggregate agg; |
| 1984 |
|
| 1985 |
slang_storage_aggregate_construct (&agg); |
| 1986 |
if (!_slang_aggregate_variable (&agg, &var->type.specifier, var->array_size, funcs, |
| 1987 |
structs)) |
| 1988 |
{ |
| 1989 |
slang_storage_aggregate_destruct (&agg); |
| 1990 |
return 0; |
| 1991 |
} |
| 1992 |
slang_storage_aggregate_destruct (&agg); |
| 1993 |
} |
| 1994 |
return 1; |
| 1995 |
} |
| 1996 |
|
| 1997 |
static int parse_init_declarator_list (slang_parse_ctx *C, slang_variable_scope *vars, |
| 1998 |
slang_struct_scope *structs, slang_function_scope *funcs) |
| 1999 |
{ |
| 2000 |
slang_fully_specified_type type; |
| 2001 |
|
| 2002 |
slang_fully_specified_type_construct (&type); |
| 2003 |
if (!parse_fully_specified_type (C, &type, structs, vars, funcs)) |
| 2004 |
{ |
| 2005 |
slang_fully_specified_type_destruct (&type); |
| 2006 |
return 0; |
| 2007 |
} |
| 2008 |
do |
| 2009 |
{ |
| 2010 |
if (!parse_init_declarator (C, &type, vars, structs, funcs)) |
| 2011 |
{ |
| 2012 |
slang_fully_specified_type_destruct (&type); |
| 2013 |
return 0; |
| 2014 |
} |
| 2015 |
} |
| 2016 |
while (*C->I++ == DECLARATOR_NEXT); |
| 2017 |
slang_fully_specified_type_destruct (&type); |
| 2018 |
return 1; |
| 2019 |
} |
| 2020 |
|
| 2021 |
static int parse_function (slang_parse_ctx *C, int definition, slang_struct_scope *structs, |
| 2022 |
slang_function_scope *funcs, slang_variable_scope *scope, slang_function **parsed_func_ret) |
| 2023 |
{ |
| 2024 |
slang_function parsed_func, *found_func; |
| 2025 |
|
| 2026 |
/* parse function definition/declaration */ |
| 2027 |
slang_function_construct (&parsed_func); |
| 2028 |
if (definition) |
| 2029 |
{ |
| 2030 |
if (!parse_function_definition (C, &parsed_func, structs, scope, funcs)) |
| 2031 |
{ |
| 2032 |
slang_function_destruct (&parsed_func); |
| 2033 |
return 0; |
| 2034 |
} |
| 2035 |
} |
| 2036 |
else |
| 2037 |
{ |
| 2038 |
if (!parse_function_prototype (C, &parsed_func, structs, scope, funcs)) |
| 2039 |
{ |
| 2040 |
slang_function_destruct (&parsed_func); |
| 2041 |
return 0; |
| 2042 |
} |
| 2043 |
} |
| 2044 |
|
| 2045 |
/* find a function with a prototype matching the parsed one - only the current scope |
| 2046 |
is being searched to allow built-in function overriding */ |
| 2047 |
found_func = slang_function_scope_find (funcs, &parsed_func, 0); |
| 2048 |
if (found_func == NULL) |
| 2049 |
{ |
| 2050 |
/* add the parsed function to the function list */ |
| 2051 |
funcs->functions = (slang_function *) slang_alloc_realloc (funcs->functions, |
| 2052 |
funcs->num_functions * sizeof (slang_function), (funcs->num_functions + 1) * sizeof ( |
| 2053 |
slang_function)); |
| 2054 |
if (funcs->functions == NULL) |
| 2055 |
{ |
| 2056 |
slang_info_log_memory (C->L); |
| 2057 |
slang_function_destruct (&parsed_func); |
| 2058 |
return 0; |
| 2059 |
} |
| 2060 |
funcs->functions[funcs->num_functions] = parsed_func; |
| 2061 |
funcs->num_functions++; |
| 2062 |
|
| 2063 |
/* return the newly parsed function */ |
| 2064 |
*parsed_func_ret = funcs->functions + funcs->num_functions - 1; |
| 2065 |
} |
| 2066 |
else |
| 2067 |
{ |
| 2068 |
/* TODO: check function return type qualifiers and specifiers */ |
| 2069 |
if (definition) |
| 2070 |
{ |
| 2071 |
/* destroy the existing function declaration and replace it with the new one */ |
| 2072 |
if (found_func->body != NULL) |
| 2073 |
{ |
| 2074 |
slang_info_log_error (C->L, "%s: function already has a body", |
| 2075 |
parsed_func.header.name); |
| 2076 |
slang_function_destruct (&parsed_func); |
| 2077 |
return 0; |
| 2078 |
} |
| 2079 |
slang_function_destruct (found_func); |
| 2080 |
*found_func = parsed_func; |
| 2081 |
} |
| 2082 |
else |
| 2083 |
{ |
| 2084 |
/* another declaration of the same function prototype - ignore it */ |
| 2085 |
slang_function_destruct (&parsed_func); |
| 2086 |
} |
| 2087 |
|
| 2088 |
/* return the found function */ |
| 2089 |
*parsed_func_ret = found_func; |
| 2090 |
} |
| 2091 |
|
| 2092 |
/* assemble the parsed function */ |
| 2093 |
if (definition) |
| 2094 |
{ |
| 2095 |
static int x = 0; |
| 2096 |
static |
| 2097 |
slang_assembly_file file; |
| 2098 |
slang_assembly_name_space space; |
| 2099 |
x++; |
| 2100 |
if (x == 1) |
| 2101 |
slang_assembly_file_construct (&file); |
| 2102 |
space.funcs = funcs; |
| 2103 |
space.structs = structs; |
| 2104 |
space.vars = scope; |
| 2105 |
if (x == 1) |
| 2106 |
xxx_first (&file); |
| 2107 |
(**parsed_func_ret).address = file.count; |
| 2108 |
if (!_slang_assemble_function (&file, *parsed_func_ret, &space)) |
| 2109 |
{ |
| 2110 |
slang_assembly_file_destruct (&file); |
| 2111 |
return 0; |
| 2112 |
} |
| 2113 |
if (slang_string_compare ("main", (**parsed_func_ret).header.name) == 0) |
| 2114 |
{ |
| 2115 |
xxx_prolog (&file, (**parsed_func_ret).address); |
| 2116 |
_slang_execute (&file); |
| 2117 |
slang_assembly_file_destruct (&file); |
| 2118 |
exit (0); |
| 2119 |
} |
| 2120 |
} |
| 2121 |
return 1; |
| 2122 |
} |
| 2123 |
|
| 2124 |
/* declaration */ |
| 2125 |
#define DECLARATION_FUNCTION_PROTOTYPE 1 |
| 2126 |
#define DECLARATION_INIT_DECLARATOR_LIST 2 |
| 2127 |
|
| 2128 |
static int parse_declaration (slang_parse_ctx *C, slang_variable_scope *scope, |
| 2129 |
slang_struct_scope *structs, slang_function_scope *funcs) |
| 2130 |
{ |
| 2131 |
slang_function *dummy_func; |
| 2132 |
|
| 2133 |
switch (*C->I++) |
| 2134 |
{ |
| 2135 |
case DECLARATION_INIT_DECLARATOR_LIST: |
| 2136 |
if (!parse_init_declarator_list (C, scope, structs, funcs)) |
| 2137 |
return 0; |
| 2138 |
break; |
| 2139 |
case DECLARATION_FUNCTION_PROTOTYPE: |
| 2140 |
if (!parse_function (C, 0, structs, funcs, scope, &dummy_func)) |
| 2141 |
return 0; |
| 2142 |
break; |
| 2143 |
default: |
| 2144 |
return 0; |
| 2145 |
} |
| 2146 |
return 1; |
| 2147 |
} |
| 2148 |
|
| 2149 |
/* external declaration */ |
| 2150 |
#define EXTERNAL_NULL 0 |
| 2151 |
#define EXTERNAL_FUNCTION_DEFINITION 1 |
| 2152 |
#define EXTERNAL_DECLARATION 2 |
| 2153 |
|
| 2154 |
static int parse_translation_unit (slang_parse_ctx *C, slang_translation_unit *unit) |
| 2155 |
{ |
| 2156 |
while (*C->I != EXTERNAL_NULL) |
| 2157 |
{ |
| 2158 |
slang_function *func; |
| 2159 |
|
| 2160 |
switch (*C->I++) |
| 2161 |
{ |
| 2162 |
case EXTERNAL_FUNCTION_DEFINITION: |
| 2163 |
if (!parse_function (C, 1, &unit->structs, &unit->functions, &unit->globals, &func)) |
| 2164 |
return 0; |
| 2165 |
break; |
| 2166 |
case EXTERNAL_DECLARATION: |
| 2167 |
if (!parse_declaration (C, &unit->globals, &unit->structs, &unit->functions)) |
| 2168 |
return 0; |
| 2169 |
break; |
| 2170 |
default: |
| 2171 |
return 0; |
| 2172 |
} |
| 2173 |
} |
| 2174 |
C->I++; |
| 2175 |
return 1; |
| 2176 |
} |
| 2177 |
|
| 2178 |
static int compile_binary (const byte *prod, slang_translation_unit *unit, slang_unit_type type, |
| 2179 |
slang_info_log *log, slang_translation_unit *builtins) |
| 2180 |
{ |
| 2181 |
slang_parse_ctx C; |
| 2182 |
|
| 2183 |
/* set-up parse context */ |
| 2184 |
C.I = prod; |
| 2185 |
C.L = log; |
| 2186 |
C.parsing_builtin = builtins == NULL; |
| 2187 |
|
| 2188 |
if (!check_revision (&C)) |
| 2189 |
return 0; |
| 2190 |
|
| 2191 |
/* create translation unit object */ |
| 2192 |
slang_translation_unit_construct (unit); |
| 2193 |
unit->type = type; |
| 2194 |
|
| 2195 |
if (builtins != NULL) |
| 2196 |
{ |
| 2197 |
/* link to built-in functions */ |
| 2198 |
builtins[1].functions.outer_scope = &builtins[0].functions; |
| 2199 |
builtins[2].functions.outer_scope = &builtins[1].functions; |
| 2200 |
unit->functions.outer_scope = &builtins[2].functions; |
| 2201 |
|
| 2202 |
/* link to built-in variables - core unit does not define any */ |
| 2203 |
builtins[2].globals.outer_scope = &builtins[1].globals; |
| 2204 |
unit->globals.outer_scope = &builtins[2].globals; |
| 2205 |
|
| 2206 |
/* link to built-in structure typedefs - only in common unit */ |
| 2207 |
unit->structs.outer_scope = &builtins[1].structs; |
| 2208 |
} |
| 2209 |
|
| 2210 |
/* parse translation unit */ |
| 2211 |
if (!parse_translation_unit (&C, unit)) |
| 2212 |
{ |
| 2213 |
slang_translation_unit_destruct (unit); |
| 2214 |
return 0; |
| 2215 |
} |
| 2216 |
|
| 2217 |
return 1; |
| 2218 |
} |
| 2219 |
|
| 2220 |
static int compile_with_grammar (grammar id, const char *source, slang_translation_unit *unit, |
| 2221 |
slang_unit_type type, slang_info_log *log, slang_translation_unit *builtins) |
| 2222 |
{ |
| 2223 |
byte *prod; |
| 2224 |
unsigned int size, start, version; |
| 2225 |
|
| 2226 |
/* retrieve version */ |
| 2227 |
if (!_slang_preprocess_version (source, &version, &start, log)) |
| 2228 |
return 0; |
| 2229 |
|
| 2230 |
/* check the syntax */ |
| 2231 |
if (!grammar_fast_check (id, (const byte *) source + start, &prod, &size, 65536)) |
| 2232 |
{ |
| 2233 |
char buf[1024]; |
| 2234 |
unsigned int pos; |
| 2235 |
grammar_get_last_error ( (unsigned char*) buf, 1024, (int*) &pos); |
| 2236 |
slang_info_log_error (log, buf); |
| 2237 |
return 0; |
| 2238 |
} |
| 2239 |
|
| 2240 |
if (!compile_binary (prod, unit, type, log, builtins)) |
| 2241 |
{ |
| 2242 |
grammar_alloc_free (prod); |
| 2243 |
return 0; |
| 2244 |
} |
| 2245 |
|
| 2246 |
grammar_alloc_free (prod); |
| 2247 |
return 1; |
| 2248 |
} |
| 2249 |
|
| 2250 |
static const char *slang_shader_syn = |
| 2251 |
#include "library/slang_shader_syn.h" |
| 2252 |
; |
| 2253 |
/* |
| 2254 |
static const byte slang_core_gc_bin[] = { |
| 2255 |
#include "library/slang_core_gc_bin.h" |
| 2256 |
};*/ |
| 2257 |
static const byte slang_core_gc[] = { |
| 2258 |
#include "library/slang_core_gc.h" |
| 2259 |
}; |
| 2260 |
|
| 2261 |
static const byte slang_common_builtin_gc_bin[] = { |
| 2262 |
#include "library/slang_common_builtin_gc_bin.h" |
| 2263 |
}; |
| 2264 |
|
| 2265 |
static const byte slang_fragment_builtin_gc_bin[] = { |
| 2266 |
#include "library/slang_fragment_builtin_gc_bin.h" |
| 2267 |
}; |
| 2268 |
|
| 2269 |
static const byte slang_vertex_builtin_gc_bin[] = { |
| 2270 |
#include "library/slang_vertex_builtin_gc_bin.h" |
| 2271 |
}; |
| 2272 |
|
| 2273 |
int _slang_compile (const char *source, slang_translation_unit *unit, slang_unit_type type, |
| 2274 |
slang_info_log *log) |
| 2275 |
{ |
| 2276 |
grammar id; |
| 2277 |
slang_translation_unit builtin_units[3]; |
| 2278 |
slang_translation_unit *builtins = NULL; |
| 2279 |
|
| 2280 |
/* load slang grammar */ |
| 2281 |
id = grammar_load_from_text ((const byte *) slang_shader_syn); |
| 2282 |
if (id == 0) |
| 2283 |
{ |
| 2284 |
char buf[1024]; |
| 2285 |
unsigned int pos; |
| 2286 |
grammar_get_last_error ( (unsigned char*) buf, 1024, (int*) &pos); |
| 2287 |
slang_info_log_error (log, buf); |
| 2288 |
return 0; |
| 2289 |
} |
| 2290 |
|
| 2291 |
/* set shader type - the syntax is slightly different for different shaders */ |
| 2292 |
if (type == slang_unit_fragment_shader || type == slang_unit_fragment_builtin) |
| 2293 |
grammar_set_reg8 (id, (const byte *) "shader_type", 1); |
| 2294 |
else |
| 2295 |
grammar_set_reg8 (id, (const byte *) "shader_type", 2); |
| 2296 |
|
| 2297 |
/* enable language extensions */ |
| 2298 |
grammar_set_reg8 (id, (const byte *) "parsing_builtin", 1); |
| 2299 |
|
| 2300 |
/* if parsing user-specified shader, load built-in library */ |
| 2301 |
if (type == slang_unit_fragment_shader || type == slang_unit_vertex_shader) |
| 2302 |
{ |
| 2303 |
/*if (!compile_binary (slang_core_gc_bin, builtin_units, |
| 2304 |
slang_unit_fragment_builtin, log, NULL))*/ |
| 2305 |
if (!compile_with_grammar (id, (const char*) slang_core_gc, builtin_units, slang_unit_fragment_builtin, |
| 2306 |
log, NULL)) |
| 2307 |
{ |
| 2308 |
grammar_destroy (id); |
| 2309 |
return 0; |
| 2310 |
} |
| 2311 |
if (!compile_binary (slang_common_builtin_gc_bin, builtin_units + 1, |
| 2312 |
slang_unit_fragment_builtin, log, NULL)) |
| 2313 |
{ |
| 2314 |
slang_translation_unit_destruct (builtin_units); |
| 2315 |
grammar_destroy (id); |
| 2316 |
return 0; |
| 2317 |
} |
| 2318 |
if (type == slang_unit_fragment_shader) |
| 2319 |
{ |
| 2320 |
if (!compile_binary (slang_fragment_builtin_gc_bin, builtin_units + 2, |
| 2321 |
slang_unit_fragment_builtin, log, NULL)) |
| 2322 |
{ |
| 2323 |
slang_translation_unit_destruct (builtin_units); |
| 2324 |
slang_translation_unit_destruct (builtin_units + 1); |
| 2325 |
grammar_destroy (id); |
| 2326 |
return 0; |
| 2327 |
} |
| 2328 |
} |
| 2329 |
else if (type == slang_unit_vertex_shader) |
| 2330 |
{ |
| 2331 |
if (!compile_binary (slang_vertex_builtin_gc_bin, builtin_units + 2, |
| 2332 |
slang_unit_vertex_builtin, log, NULL)) |
| 2333 |
{ |
| 2334 |
slang_translation_unit_destruct (builtin_units); |
| 2335 |
slang_translation_unit_destruct (builtin_units + 1); |
| 2336 |
grammar_destroy (id); |
| 2337 |
return 0; |
| 2338 |
} |
| 2339 |
} |
| 2340 |
|
| 2341 |
/* disable language extensions */ |
| 2342 |
grammar_set_reg8 (id, (const byte *) "parsing_builtin", 0); |
| 2343 |
builtins = builtin_units; |
| 2344 |
} |
| 2345 |
|
| 2346 |
/* compile the actual shader - pass-in built-in library for external shader */ |
| 2347 |
if (!compile_with_grammar (id, source, unit, type, log, builtins)) |
| 2348 |
{ |
| 2349 |
if (type == slang_unit_fragment_shader || type == slang_unit_vertex_shader) |
| 2350 |
{ |
| 2351 |
slang_translation_unit_destruct (builtin_units); |
| 2352 |
slang_translation_unit_destruct (builtin_units + 1); |
| 2353 |
slang_translation_unit_destruct (builtin_units + 2); |
| 2354 |
} |
| 2355 |
grammar_destroy (id); |
| 2356 |
return 0; |
| 2357 |
} |
| 2358 |
|
| 2359 |
/* destroy built-in library */ |
| 2360 |
if (type == slang_unit_fragment_shader || type == slang_unit_vertex_shader) |
| 2361 |
{ |
| 2362 |
slang_translation_unit_destruct (builtin_units); |
| 2363 |
slang_translation_unit_destruct (builtin_units + 1); |
| 2364 |
slang_translation_unit_destruct (builtin_units + 2); |
| 2365 |
} |
| 2366 |
grammar_destroy (id); |
| 2367 |
return 1; |
| 2368 |
} |
| 2369 |
|