/xbmc/visualizations/Goom/goom2k4-0/src/goomsl_yacc.c

http://github.com/xbmc/xbmc · C · 2997 lines · 2309 code · 404 blank · 284 comment · 303 complexity · 6616b1b5b943d68f1012fa9618055aa8 MD5 · raw file

Large files are truncated click here to view the full file

  1. /* A Bison parser, made by GNU Bison 1.875. */
  2. /* Skeleton parser for Yacc-like parsing with Bison,
  3. Copyright (C) 1984, 1989, 1990, 2000, 2001, 2002 Free Software Foundation, Inc.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 59 Temple Place - Suite 330,
  15. Boston, MA 02111-1307, USA. */
  16. /* As a special exception, when this file is copied by Bison into a
  17. Bison output file, you may use that output file without restriction.
  18. This special exception was added by the Free Software Foundation
  19. in version 1.24 of Bison. */
  20. /* Written by Richard Stallman by simplifying the original so called
  21. ``semantic'' parser. */
  22. /* All symbols defined below should begin with yy or YY, to avoid
  23. infringing on user name space. This should be done even for local
  24. variables, as they might otherwise be expanded by user macros.
  25. There are some unavoidable exceptions within include files to
  26. define necessary library symbols; they are noted "INFRINGES ON
  27. USER NAME SPACE" below. */
  28. /* Identify Bison output. */
  29. #define YYBISON 1
  30. /* Skeleton name. */
  31. #define YYSKELETON_NAME "yacc.c"
  32. /* Pure parsers. */
  33. #define YYPURE 0
  34. /* Using locations. */
  35. #define YYLSP_NEEDED 0
  36. /* Tokens. */
  37. #ifndef YYTOKENTYPE
  38. # define YYTOKENTYPE
  39. /* Put the tokens into the symbol table, so that GDB and other debuggers
  40. know about them. */
  41. enum yytokentype {
  42. LTYPE_INTEGER = 258,
  43. LTYPE_FLOAT = 259,
  44. LTYPE_VAR = 260,
  45. LTYPE_PTR = 261,
  46. PTR_TK = 262,
  47. INT_TK = 263,
  48. FLOAT_TK = 264,
  49. DECLARE = 265,
  50. EXTERNAL = 266,
  51. WHILE = 267,
  52. DO = 268,
  53. NOT = 269,
  54. PLUS_EQ = 270,
  55. SUB_EQ = 271,
  56. DIV_EQ = 272,
  57. MUL_EQ = 273,
  58. SUP_EQ = 274,
  59. LOW_EQ = 275,
  60. NOT_EQ = 276,
  61. STRUCT = 277,
  62. FOR = 278,
  63. IN = 279
  64. };
  65. #endif
  66. #define LTYPE_INTEGER 258
  67. #define LTYPE_FLOAT 259
  68. #define LTYPE_VAR 260
  69. #define LTYPE_PTR 261
  70. #define PTR_TK 262
  71. #define INT_TK 263
  72. #define FLOAT_TK 264
  73. #define DECLARE 265
  74. #define EXTERNAL 266
  75. #define WHILE 267
  76. #define DO 268
  77. #define NOT 269
  78. #define PLUS_EQ 270
  79. #define SUB_EQ 271
  80. #define DIV_EQ 272
  81. #define MUL_EQ 273
  82. #define SUP_EQ 274
  83. #define LOW_EQ 275
  84. #define NOT_EQ 276
  85. #define STRUCT 277
  86. #define FOR 278
  87. #define IN 279
  88. /* Copy the first part of user declarations. */
  89. #line 6 "goomsl_yacc.y"
  90. #include <stdio.h>
  91. #include <stdlib.h>
  92. #include <string.h>
  93. #include "goomsl.h"
  94. #include "goomsl_private.h"
  95. #define STRUCT_ALIGNMENT 16
  96. /* #define VERBOSE */
  97. int yylex(void);
  98. void yyerror(char *);
  99. extern GoomSL *currentGoomSL;
  100. static NodeType *nodeNew(const char *str, int type, int line_number);
  101. static NodeType *nodeClone(NodeType *node);
  102. static void nodeFreeInternals(NodeType *node);
  103. static void nodeFree(NodeType *node);
  104. static void commit_node(NodeType *node, int releaseIfTemp);
  105. static void precommit_node(NodeType *node);
  106. static NodeType *new_constInt(const char *str, int line_number);
  107. static NodeType *new_constFloat(const char *str, int line_number);
  108. static NodeType *new_constPtr(const char *str, int line_number);
  109. static NodeType *new_var(const char *str, int line_number);
  110. static NodeType *new_nop(const char *str);
  111. static NodeType *new_op(const char *str, int type, int nbOp);
  112. static int allocateLabel();
  113. static int allocateTemp();
  114. static void releaseTemp(int n);
  115. static void releaseAllTemps();
  116. static int is_tmp_expr(NodeType *node) {
  117. if (node->str) {
  118. return (!strncmp(node->str,"_i_tmp_",7))
  119. || (!strncmp(node->str,"_f_tmp_",7))
  120. || (!strncmp(node->str,"_p_tmp",7));
  121. }
  122. return 0;
  123. }
  124. /* pre: is_tmp_expr(node); */
  125. static int get_tmp_id(NodeType *node) { return atoi((node->str)+5); }
  126. static int is_commutative_expr(int itype)
  127. { /* {{{ */
  128. return (itype == INSTR_ADD)
  129. || (itype == INSTR_MUL)
  130. || (itype == INSTR_ISEQUAL);
  131. } /* }}} */
  132. static void GSL_PUT_LABEL(char *name, int line_number)
  133. { /* {{{ */
  134. #ifdef VERBOSE
  135. printf("label %s\n", name);
  136. #endif
  137. currentGoomSL->instr = gsl_instr_init(currentGoomSL, "label", INSTR_LABEL, 1, line_number);
  138. gsl_instr_add_param(currentGoomSL->instr, name, TYPE_LABEL);
  139. } /* }}} */
  140. static void GSL_PUT_JUMP(char *name, int line_number)
  141. { /* {{{ */
  142. #ifdef VERBOSE
  143. printf("jump %s\n", name);
  144. #endif
  145. currentGoomSL->instr = gsl_instr_init(currentGoomSL, "jump", INSTR_JUMP, 1, line_number);
  146. gsl_instr_add_param(currentGoomSL->instr, name, TYPE_LABEL);
  147. } /* }}} */
  148. static void GSL_PUT_JXXX(char *name, char *iname, int instr_id, int line_number)
  149. { /* {{{ */
  150. #ifdef VERBOSE
  151. printf("%s %s\n", iname, name);
  152. #endif
  153. currentGoomSL->instr = gsl_instr_init(currentGoomSL, iname, instr_id, 1, line_number);
  154. gsl_instr_add_param(currentGoomSL->instr, name, TYPE_LABEL);
  155. } /* }}} */
  156. static void GSL_PUT_JZERO(char *name,int line_number)
  157. { /* {{{ */
  158. GSL_PUT_JXXX(name,"jzero.i",INSTR_JZERO,line_number);
  159. } /* }}} */
  160. static void GSL_PUT_JNZERO(char *name, int line_number)
  161. { /* {{{ */
  162. GSL_PUT_JXXX(name,"jnzero.i",INSTR_JNZERO,line_number);
  163. } /* }}} */
  164. /* Structures Management */
  165. #define ALIGN_ADDR(_addr,_align) {\
  166. if (_align>1) {\
  167. int _dec = (_addr%_align);\
  168. if (_dec != 0) _addr += _align - _dec;\
  169. }}
  170. /* */
  171. void gsl_prepare_struct(GSL_Struct *s, int s_align, int i_align, int f_align)
  172. {
  173. int i;
  174. int consumed = 0;
  175. int iblk=0, fblk=0;
  176. s->iBlock[0].size = 0;
  177. s->iBlock[0].data = 0;
  178. s->fBlock[0].size = 0;
  179. s->fBlock[0].data = 0;
  180. /* Prepare sub-struct and calculate space needed for their storage */
  181. for (i = 0; i < s->nbFields; ++i)
  182. {
  183. if (s->fields[i]->type < FIRST_RESERVED)
  184. {
  185. int j=0;
  186. GSL_Struct *substruct = currentGoomSL->gsl_struct[s->fields[i]->type];
  187. consumed += sizeof(int); /* stocke le prefix */
  188. ALIGN_ADDR(consumed, s_align);
  189. s->fields[i]->offsetInStruct = consumed;
  190. gsl_prepare_struct(substruct, s_align, i_align, f_align);
  191. for(j=0;substruct->iBlock[j].size>0;++j) {
  192. s->iBlock[iblk].data = consumed + substruct->iBlock[j].data;
  193. s->iBlock[iblk].size = substruct->iBlock[j].size;
  194. iblk++;
  195. }
  196. for(j=0;substruct->fBlock[j].size>0;++j) {
  197. s->fBlock[fblk].data = consumed + substruct->fBlock[j].data;
  198. s->fBlock[fblk].size = substruct->fBlock[j].size;
  199. fblk++;
  200. }
  201. consumed += substruct->size;
  202. }
  203. }
  204. /* Then prepare integers */
  205. ALIGN_ADDR(consumed, i_align);
  206. for (i = 0; i < s->nbFields; ++i)
  207. {
  208. if (s->fields[i]->type == INSTR_INT)
  209. {
  210. if (s->iBlock[iblk].size == 0) {
  211. s->iBlock[iblk].size = 1;
  212. s->iBlock[iblk].data = consumed;
  213. } else {
  214. s->iBlock[iblk].size += 1;
  215. }
  216. s->fields[i]->offsetInStruct = consumed;
  217. consumed += sizeof(int);
  218. }
  219. }
  220. iblk++;
  221. s->iBlock[iblk].size = 0;
  222. s->iBlock[iblk].data = 0;
  223. /* Then prepare floats */
  224. ALIGN_ADDR(consumed, f_align);
  225. for (i = 0; i < s->nbFields; ++i)
  226. {
  227. if (s->fields[i]->type == INSTR_FLOAT)
  228. {
  229. if (s->fBlock[fblk].size == 0) {
  230. s->fBlock[fblk].size = 1;
  231. s->fBlock[fblk].data = consumed;
  232. } else {
  233. s->fBlock[fblk].size += 1;
  234. }
  235. s->fields[i]->offsetInStruct = consumed;
  236. consumed += sizeof(int);
  237. }
  238. }
  239. fblk++;
  240. s->fBlock[fblk].size = 0;
  241. s->fBlock[fblk].data = 0;
  242. /* Finally prepare pointers */
  243. ALIGN_ADDR(consumed, i_align);
  244. for (i = 0; i < s->nbFields; ++i)
  245. {
  246. if (s->fields[i]->type == INSTR_PTR)
  247. {
  248. s->fields[i]->offsetInStruct = consumed;
  249. consumed += sizeof(int);
  250. }
  251. }
  252. s->size = consumed;
  253. }
  254. /* Returns the ID of a struct from its name */
  255. int gsl_get_struct_id(const char *name) /* {{{ */
  256. {
  257. HashValue *ret = goom_hash_get(currentGoomSL->structIDS, name);
  258. if (ret != NULL) return ret->i;
  259. return -1;
  260. } /* }}} */
  261. /* Adds the definition of a struct */
  262. void gsl_add_struct(const char *name, GSL_Struct *gsl_struct) /* {{{ */
  263. {
  264. /* Prepare the struct: ie calculate internal storage format */
  265. gsl_prepare_struct(gsl_struct, STRUCT_ALIGNMENT, STRUCT_ALIGNMENT, STRUCT_ALIGNMENT);
  266. /* If the struct does not already exists */
  267. if (gsl_get_struct_id(name) < 0)
  268. {
  269. /* adds it */
  270. int id = currentGoomSL->nbStructID++;
  271. goom_hash_put_int(currentGoomSL->structIDS, name, id);
  272. if (currentGoomSL->gsl_struct_size <= id) {
  273. currentGoomSL->gsl_struct_size *= 2;
  274. currentGoomSL->gsl_struct = (GSL_Struct**)realloc(currentGoomSL->gsl_struct,
  275. sizeof(GSL_Struct*) * currentGoomSL->gsl_struct_size);
  276. }
  277. currentGoomSL->gsl_struct[id] = gsl_struct;
  278. }
  279. } /* }}} */
  280. /* Creates a field for a struct */
  281. GSL_StructField *gsl_new_struct_field(const char *name, int type)
  282. {
  283. GSL_StructField *field = (GSL_StructField*)malloc(sizeof(GSL_StructField));
  284. strcpy(field->name, name);
  285. field->type = type;
  286. return field;
  287. }
  288. /* Create as field for a struct which will be a struct itself */
  289. GSL_StructField *gsl_new_struct_field_struct(const char *name, const char *type)
  290. {
  291. GSL_StructField *field = gsl_new_struct_field(name, gsl_get_struct_id(type));
  292. if (field->type < 0) {
  293. fprintf(stderr, "ERROR: Line %d, Unknown structure: '%s'\n",
  294. currentGoomSL->num_lines, type);
  295. exit(1);
  296. }
  297. return field;
  298. }
  299. /* Creates a Struct */
  300. GSL_Struct *gsl_new_struct(GSL_StructField *field)
  301. {
  302. GSL_Struct *s = (GSL_Struct*)malloc(sizeof(GSL_Struct));
  303. s->nbFields = 1;
  304. s->fields[0] = field;
  305. return s;
  306. }
  307. /* Adds a field to a struct */
  308. void gsl_add_struct_field(GSL_Struct *s, GSL_StructField *field)
  309. {
  310. s->fields[s->nbFields++] = field;
  311. }
  312. int gsl_type_of_var(GoomHash *ns, const char *name)
  313. {
  314. char type_of[256];
  315. HashValue *hv;
  316. sprintf(type_of, "__type_of_%s", name);
  317. hv = goom_hash_get(ns, type_of);
  318. if (hv != NULL)
  319. return hv->i;
  320. fprintf(stderr, "ERROR: Unknown variable type: '%s'\n", name);
  321. return -1;
  322. }
  323. static void gsl_declare_var(GoomHash *ns, const char *name, int type, void *space)
  324. {
  325. char type_of[256];
  326. if (name[0] == '@') { ns = currentGoomSL->vars; }
  327. if (space == NULL) {
  328. switch (type) {
  329. case INSTR_INT:
  330. case INSTR_FLOAT:
  331. case INSTR_PTR:
  332. space = goom_heap_malloc_with_alignment(currentGoomSL->data_heap,
  333. sizeof(int), sizeof(int));
  334. break;
  335. case -1:
  336. fprintf(stderr, "What the fuck!\n");
  337. exit(1);
  338. default: /* On a un struct_id */
  339. space = goom_heap_malloc_with_alignment_prefixed(currentGoomSL->data_heap,
  340. currentGoomSL->gsl_struct[type]->size, STRUCT_ALIGNMENT, sizeof(int));
  341. }
  342. }
  343. goom_hash_put_ptr(ns, name, (void*)space);
  344. sprintf(type_of, "__type_of_%s", name);
  345. goom_hash_put_int(ns, type_of, type);
  346. /* Ensuite le hack: on ajoute les champs en tant que variables. */
  347. if (type < FIRST_RESERVED)
  348. {
  349. int i;
  350. GSL_Struct *gsl_struct = currentGoomSL->gsl_struct[type];
  351. ((int*)space)[-1] = type; /* stockage du type dans le prefixe de structure */
  352. for (i = 0; i < gsl_struct->nbFields; ++i)
  353. {
  354. char full_name[256];
  355. char *cspace = (char*)space + gsl_struct->fields[i]->offsetInStruct;
  356. sprintf(full_name, "%s.%s", name, gsl_struct->fields[i]->name);
  357. gsl_declare_var(ns, full_name, gsl_struct->fields[i]->type, cspace);
  358. }
  359. }
  360. }
  361. /* Declare a variable which will be a struct */
  362. static void gsl_struct_decl(GoomHash *namespace, const char *struct_name, const char *name)
  363. {
  364. int struct_id = gsl_get_struct_id(struct_name);
  365. gsl_declare_var(namespace, name, struct_id, NULL);
  366. }
  367. static void gsl_float_decl_global(const char *name)
  368. {
  369. gsl_declare_var(currentGoomSL->vars, name, INSTR_FLOAT, NULL);
  370. }
  371. static void gsl_int_decl_global(const char *name)
  372. {
  373. gsl_declare_var(currentGoomSL->vars, name, INSTR_INT, NULL);
  374. }
  375. static void gsl_ptr_decl_global(const char *name)
  376. {
  377. gsl_declare_var(currentGoomSL->vars, name, INSTR_PTR, NULL);
  378. }
  379. static void gsl_struct_decl_global_from_id(const char *name, int id)
  380. {
  381. gsl_declare_var(currentGoomSL->vars, name, id, NULL);
  382. }
  383. /* FLOAT */
  384. static void gsl_float_decl_local(const char *name)
  385. {
  386. gsl_declare_var(currentGoomSL->namespaces[currentGoomSL->currentNS], name, INSTR_FLOAT, NULL);
  387. }
  388. /* INT */
  389. static void gsl_int_decl_local(const char *name)
  390. {
  391. gsl_declare_var(currentGoomSL->namespaces[currentGoomSL->currentNS], name, INSTR_INT, NULL);
  392. }
  393. /* PTR */
  394. static void gsl_ptr_decl_local(const char *name)
  395. {
  396. gsl_declare_var(currentGoomSL->namespaces[currentGoomSL->currentNS], name, INSTR_PTR, NULL);
  397. }
  398. /* STRUCT */
  399. static void gsl_struct_decl_local(const char *struct_name, const char *name)
  400. {
  401. gsl_struct_decl(currentGoomSL->namespaces[currentGoomSL->currentNS],struct_name,name);
  402. }
  403. static void commit_test2(NodeType *set,const char *type, int instr);
  404. static NodeType *new_call(const char *name, NodeType *affect_list);
  405. /* SETTER */
  406. static NodeType *new_set(NodeType *lvalue, NodeType *expression)
  407. { /* {{{ */
  408. NodeType *set = new_op("set", OPR_SET, 2);
  409. set->unode.opr.op[0] = lvalue;
  410. set->unode.opr.op[1] = expression;
  411. return set;
  412. } /* }}} */
  413. static void commit_set(NodeType *set)
  414. { /* {{{ */
  415. commit_test2(set,"set",INSTR_SET);
  416. } /* }}} */
  417. /* PLUS_EQ */
  418. static NodeType *new_plus_eq(NodeType *lvalue, NodeType *expression) /* {{{ */
  419. {
  420. NodeType *set = new_op("plus_eq", OPR_PLUS_EQ, 2);
  421. set->unode.opr.op[0] = lvalue;
  422. set->unode.opr.op[1] = expression;
  423. return set;
  424. }
  425. static void commit_plus_eq(NodeType *set)
  426. {
  427. precommit_node(set->unode.opr.op[1]);
  428. #ifdef VERBOSE
  429. printf("add %s %s\n", set->unode.opr.op[0]->str, set->unode.opr.op[1]->str);
  430. #endif
  431. currentGoomSL->instr = gsl_instr_init(currentGoomSL, "add", INSTR_ADD, 2, set->line_number);
  432. commit_node(set->unode.opr.op[0],0);
  433. commit_node(set->unode.opr.op[1],1);
  434. } /* }}} */
  435. /* SUB_EQ */
  436. static NodeType *new_sub_eq(NodeType *lvalue, NodeType *expression) /* {{{ */
  437. {
  438. NodeType *set = new_op("sub_eq", OPR_SUB_EQ, 2);
  439. set->unode.opr.op[0] = lvalue;
  440. set->unode.opr.op[1] = expression;
  441. return set;
  442. }
  443. static void commit_sub_eq(NodeType *set)
  444. {
  445. precommit_node(set->unode.opr.op[1]);
  446. #ifdef VERBOSE
  447. printf("sub %s %s\n", set->unode.opr.op[0]->str, set->unode.opr.op[1]->str);
  448. #endif
  449. currentGoomSL->instr = gsl_instr_init(currentGoomSL, "sub", INSTR_SUB, 2, set->line_number);
  450. commit_node(set->unode.opr.op[0],0);
  451. commit_node(set->unode.opr.op[1],1);
  452. } /* }}} */
  453. /* MUL_EQ */
  454. static NodeType *new_mul_eq(NodeType *lvalue, NodeType *expression) /* {{{ */
  455. {
  456. NodeType *set = new_op("mul_eq", OPR_MUL_EQ, 2);
  457. set->unode.opr.op[0] = lvalue;
  458. set->unode.opr.op[1] = expression;
  459. return set;
  460. }
  461. static void commit_mul_eq(NodeType *set)
  462. {
  463. precommit_node(set->unode.opr.op[1]);
  464. #ifdef VERBOSE
  465. printf("mul %s %s\n", set->unode.opr.op[0]->str, set->unode.opr.op[1]->str);
  466. #endif
  467. currentGoomSL->instr = gsl_instr_init(currentGoomSL, "mul", INSTR_MUL, 2, set->line_number);
  468. commit_node(set->unode.opr.op[0],0);
  469. commit_node(set->unode.opr.op[1],1);
  470. } /* }}} */
  471. /* DIV_EQ */
  472. static NodeType *new_div_eq(NodeType *lvalue, NodeType *expression) /* {{{ */
  473. {
  474. NodeType *set = new_op("div_eq", OPR_DIV_EQ, 2);
  475. set->unode.opr.op[0] = lvalue;
  476. set->unode.opr.op[1] = expression;
  477. return set;
  478. }
  479. static void commit_div_eq(NodeType *set)
  480. {
  481. precommit_node(set->unode.opr.op[1]);
  482. #ifdef VERBOSE
  483. printf("div %s %s\n", set->unode.opr.op[0]->str, set->unode.opr.op[1]->str);
  484. #endif
  485. currentGoomSL->instr = gsl_instr_init(currentGoomSL, "div", INSTR_DIV, 2, set->line_number);
  486. commit_node(set->unode.opr.op[0],0);
  487. commit_node(set->unode.opr.op[1],1);
  488. } /* }}} */
  489. /* commodity method for add, mult, ... */
  490. static void precommit_expr(NodeType *expr, const char *type, int instr_id)
  491. { /* {{{ */
  492. NodeType *tmp, *tmpcpy;
  493. int toAdd;
  494. /* compute "left" and "right" */
  495. switch (expr->unode.opr.nbOp) {
  496. case 2:
  497. precommit_node(expr->unode.opr.op[1]);
  498. case 1:
  499. precommit_node(expr->unode.opr.op[0]);
  500. }
  501. if (is_tmp_expr(expr->unode.opr.op[0])) {
  502. tmp = expr->unode.opr.op[0];
  503. toAdd = 1;
  504. }
  505. else if (is_commutative_expr(instr_id) && (expr->unode.opr.nbOp==2) && is_tmp_expr(expr->unode.opr.op[1])) {
  506. tmp = expr->unode.opr.op[1];
  507. toAdd = 0;
  508. }
  509. else {
  510. char stmp[256];
  511. /* declare a temporary variable to store the result */
  512. if (expr->unode.opr.op[0]->type == CONST_INT_NODE) {
  513. sprintf(stmp,"_i_tmp_%i",allocateTemp());
  514. gsl_int_decl_global(stmp);
  515. }
  516. else if (expr->unode.opr.op[0]->type == CONST_FLOAT_NODE) {
  517. sprintf(stmp,"_f_tmp%i",allocateTemp());
  518. gsl_float_decl_global(stmp);
  519. }
  520. else if (expr->unode.opr.op[0]->type == CONST_PTR_NODE) {
  521. sprintf(stmp,"_p_tmp%i",allocateTemp());
  522. gsl_ptr_decl_global(stmp);
  523. }
  524. else {
  525. int type = gsl_type_of_var(expr->unode.opr.op[0]->vnamespace, expr->unode.opr.op[0]->str);
  526. if (type == INSTR_FLOAT) {
  527. sprintf(stmp,"_f_tmp_%i",allocateTemp());
  528. gsl_float_decl_global(stmp);
  529. }
  530. else if (type == INSTR_PTR) {
  531. sprintf(stmp,"_p_tmp_%i",allocateTemp());
  532. gsl_ptr_decl_global(stmp);
  533. }
  534. else if (type == INSTR_INT) {
  535. sprintf(stmp,"_i_tmp_%i",allocateTemp());
  536. gsl_int_decl_global(stmp);
  537. }
  538. else if (type == -1) {
  539. fprintf(stderr, "ERROR: Line %d, Could not find variable '%s'\n",
  540. expr->line_number, expr->unode.opr.op[0]->str);
  541. exit(1);
  542. }
  543. else { /* type is a struct_id */
  544. sprintf(stmp,"_s_tmp_%i",allocateTemp());
  545. gsl_struct_decl_global_from_id(stmp,type);
  546. }
  547. }
  548. tmp = new_var(stmp,expr->line_number);
  549. /* set the tmp to the value of "op1" */
  550. tmpcpy = nodeClone(tmp);
  551. commit_node(new_set(tmp,expr->unode.opr.op[0]),0);
  552. toAdd = 1;
  553. tmp = tmpcpy;
  554. }
  555. /* add op2 to tmp */
  556. #ifdef VERBOSE
  557. if (expr->unode.opr.nbOp == 2)
  558. printf("%s %s %s\n", type, tmp->str, expr->unode.opr.op[toAdd]->str);
  559. else
  560. printf("%s %s\n", type, tmp->str);
  561. #endif
  562. currentGoomSL->instr = gsl_instr_init(currentGoomSL, type, instr_id, expr->unode.opr.nbOp, expr->line_number);
  563. tmpcpy = nodeClone(tmp);
  564. commit_node(tmp,0);
  565. if (expr->unode.opr.nbOp == 2) {
  566. commit_node(expr->unode.opr.op[toAdd],1);
  567. }
  568. /* redefine the ADD node now as the computed variable */
  569. nodeFreeInternals(expr);
  570. *expr = *tmpcpy;
  571. free(tmpcpy);
  572. } /* }}} */
  573. static NodeType *new_expr1(const char *name, int id, NodeType *expr1)
  574. { /* {{{ */
  575. NodeType *add = new_op(name, id, 1);
  576. add->unode.opr.op[0] = expr1;
  577. return add;
  578. } /* }}} */
  579. static NodeType *new_expr2(const char *name, int id, NodeType *expr1, NodeType *expr2)
  580. { /* {{{ */
  581. NodeType *add = new_op(name, id, 2);
  582. add->unode.opr.op[0] = expr1;
  583. add->unode.opr.op[1] = expr2;
  584. return add;
  585. } /* }}} */
  586. /* ADD */
  587. static NodeType *new_add(NodeType *expr1, NodeType *expr2) { /* {{{ */
  588. return new_expr2("add", OPR_ADD, expr1, expr2);
  589. }
  590. static void precommit_add(NodeType *add) {
  591. precommit_expr(add,"add",INSTR_ADD);
  592. } /* }}} */
  593. /* SUB */
  594. static NodeType *new_sub(NodeType *expr1, NodeType *expr2) { /* {{{ */
  595. return new_expr2("sub", OPR_SUB, expr1, expr2);
  596. }
  597. static void precommit_sub(NodeType *sub) {
  598. precommit_expr(sub,"sub",INSTR_SUB);
  599. } /* }}} */
  600. /* NEG */
  601. static NodeType *new_neg(NodeType *expr) { /* {{{ */
  602. NodeType *zeroConst = NULL;
  603. if (expr->type == CONST_INT_NODE)
  604. zeroConst = new_constInt("0", currentGoomSL->num_lines);
  605. else if (expr->type == CONST_FLOAT_NODE)
  606. zeroConst = new_constFloat("0.0", currentGoomSL->num_lines);
  607. else if (expr->type == CONST_PTR_NODE) {
  608. fprintf(stderr, "ERROR: Line %d, Could not negate const pointer.\n",
  609. currentGoomSL->num_lines);
  610. exit(1);
  611. }
  612. else {
  613. int type = gsl_type_of_var(expr->vnamespace, expr->str);
  614. if (type == INSTR_FLOAT)
  615. zeroConst = new_constFloat("0.0", currentGoomSL->num_lines);
  616. else if (type == INSTR_PTR) {
  617. fprintf(stderr, "ERROR: Line %d, Could not negate pointer.\n",
  618. currentGoomSL->num_lines);
  619. exit(1);
  620. }
  621. else if (type == INSTR_INT)
  622. zeroConst = new_constInt("0", currentGoomSL->num_lines);
  623. else if (type == -1) {
  624. fprintf(stderr, "ERROR: Line %d, Could not find variable '%s'\n",
  625. expr->line_number, expr->unode.opr.op[0]->str);
  626. exit(1);
  627. }
  628. else { /* type is a struct_id */
  629. fprintf(stderr, "ERROR: Line %d, Could not negate struct '%s'\n",
  630. expr->line_number, expr->str);
  631. exit(1);
  632. }
  633. }
  634. return new_expr2("sub", OPR_SUB, zeroConst, expr);
  635. }
  636. /* }}} */
  637. /* MUL */
  638. static NodeType *new_mul(NodeType *expr1, NodeType *expr2) { /* {{{ */
  639. return new_expr2("mul", OPR_MUL, expr1, expr2);
  640. }
  641. static void precommit_mul(NodeType *mul) {
  642. precommit_expr(mul,"mul",INSTR_MUL);
  643. } /* }}} */
  644. /* DIV */
  645. static NodeType *new_div(NodeType *expr1, NodeType *expr2) { /* {{{ */
  646. return new_expr2("div", OPR_DIV, expr1, expr2);
  647. }
  648. static void precommit_div(NodeType *mul) {
  649. precommit_expr(mul,"div",INSTR_DIV);
  650. } /* }}} */
  651. /* CALL EXPRESSION */
  652. static NodeType *new_call_expr(const char *name, NodeType *affect_list) { /* {{{ */
  653. NodeType *call = new_call(name,affect_list);
  654. NodeType *node = new_expr1(name, OPR_CALL_EXPR, call);
  655. node->vnamespace = gsl_find_namespace(name);
  656. if (node->vnamespace == NULL)
  657. fprintf(stderr, "ERROR: Line %d, No return type for: '%s'\n", currentGoomSL->num_lines, name);
  658. return node;
  659. }
  660. static void precommit_call_expr(NodeType *call) {
  661. char stmp[256];
  662. NodeType *tmp,*tmpcpy;
  663. int type = gsl_type_of_var(call->vnamespace, call->str);
  664. if (type == INSTR_FLOAT) {
  665. sprintf(stmp,"_f_tmp_%i",allocateTemp());
  666. gsl_float_decl_global(stmp);
  667. }
  668. else if (type == INSTR_PTR) {
  669. sprintf(stmp,"_p_tmp_%i",allocateTemp());
  670. gsl_ptr_decl_global(stmp);
  671. }
  672. else if (type == INSTR_INT) {
  673. sprintf(stmp,"_i_tmp_%i",allocateTemp());
  674. gsl_int_decl_global(stmp);
  675. }
  676. else if (type == -1) {
  677. fprintf(stderr, "ERROR: Line %d, Could not find variable '%s'\n",
  678. call->line_number, call->str);
  679. exit(1);
  680. }
  681. else { /* type is a struct_id */
  682. sprintf(stmp,"_s_tmp_%i",allocateTemp());
  683. gsl_struct_decl_global_from_id(stmp,type);
  684. }
  685. tmp = new_var(stmp,call->line_number);
  686. commit_node(call->unode.opr.op[0],0);
  687. tmpcpy = nodeClone(tmp);
  688. commit_node(new_set(tmp,new_var(call->str,call->line_number)),0);
  689. nodeFreeInternals(call);
  690. *call = *tmpcpy;
  691. free(tmpcpy);
  692. } /* }}} */
  693. static void commit_test2(NodeType *set,const char *type, int instr)
  694. { /* {{{ */
  695. NodeType *tmp;
  696. char stmp[256];
  697. precommit_node(set->unode.opr.op[0]);
  698. precommit_node(set->unode.opr.op[1]);
  699. tmp = set->unode.opr.op[0];
  700. stmp[0] = 0;
  701. if (set->unode.opr.op[0]->type == CONST_INT_NODE) {
  702. sprintf(stmp,"_i_tmp_%i",allocateTemp());
  703. gsl_int_decl_global(stmp);
  704. }
  705. else if (set->unode.opr.op[0]->type == CONST_FLOAT_NODE) {
  706. sprintf(stmp,"_f_tmp%i",allocateTemp());
  707. gsl_float_decl_global(stmp);
  708. }
  709. else if (set->unode.opr.op[0]->type == CONST_PTR_NODE) {
  710. sprintf(stmp,"_p_tmp%i",allocateTemp());
  711. gsl_ptr_decl_global(stmp);
  712. }
  713. if (stmp[0]) {
  714. NodeType *tmpcpy;
  715. tmp = new_var(stmp, set->line_number);
  716. tmpcpy = nodeClone(tmp);
  717. commit_node(new_set(tmp,set->unode.opr.op[0]),0);
  718. tmp = tmpcpy;
  719. }
  720. #ifdef VERBOSE
  721. printf("%s %s %s\n", type, tmp->str, set->unode.opr.op[1]->str);
  722. #endif
  723. currentGoomSL->instr = gsl_instr_init(currentGoomSL, type, instr, 2, set->line_number);
  724. commit_node(tmp,instr!=INSTR_SET);
  725. commit_node(set->unode.opr.op[1],1);
  726. } /* }}} */
  727. /* NOT */
  728. static NodeType *new_not(NodeType *expr1) { /* {{{ */
  729. return new_expr1("not", OPR_NOT, expr1);
  730. }
  731. static void commit_not(NodeType *set)
  732. {
  733. commit_node(set->unode.opr.op[0],0);
  734. #ifdef VERBOSE
  735. printf("not\n");
  736. #endif
  737. currentGoomSL->instr = gsl_instr_init(currentGoomSL, "not", INSTR_NOT, 1, set->line_number);
  738. gsl_instr_add_param(currentGoomSL->instr, "|dummy|", TYPE_LABEL);
  739. } /* }}} */
  740. /* EQU */
  741. static NodeType *new_equ(NodeType *expr1, NodeType *expr2) { /* {{{ */
  742. return new_expr2("isequal", OPR_EQU, expr1, expr2);
  743. }
  744. static void commit_equ(NodeType *mul) {
  745. commit_test2(mul,"isequal",INSTR_ISEQUAL);
  746. } /* }}} */
  747. /* INF */
  748. static NodeType *new_low(NodeType *expr1, NodeType *expr2) { /* {{{ */
  749. return new_expr2("islower", OPR_LOW, expr1, expr2);
  750. }
  751. static void commit_low(NodeType *mul) {
  752. commit_test2(mul,"islower",INSTR_ISLOWER);
  753. } /* }}} */
  754. /* WHILE */
  755. static NodeType *new_while(NodeType *expression, NodeType *instr) { /* {{{ */
  756. NodeType *node = new_op("while", OPR_WHILE, 2);
  757. node->unode.opr.op[0] = expression;
  758. node->unode.opr.op[1] = instr;
  759. return node;
  760. }
  761. static void commit_while(NodeType *node)
  762. {
  763. int lbl = allocateLabel();
  764. char start_while[1024], test_while[1024];
  765. sprintf(start_while, "|start_while_%d|", lbl);
  766. sprintf(test_while, "|test_while_%d|", lbl);
  767. GSL_PUT_JUMP(test_while,node->line_number);
  768. GSL_PUT_LABEL(start_while,node->line_number);
  769. /* code */
  770. commit_node(node->unode.opr.op[1],0);
  771. GSL_PUT_LABEL(test_while,node->line_number);
  772. commit_node(node->unode.opr.op[0],0);
  773. GSL_PUT_JNZERO(start_while,node->line_number);
  774. } /* }}} */
  775. /* FOR EACH */
  776. static NodeType *new_static_foreach(NodeType *var, NodeType *var_list, NodeType *instr) { /* {{{ */
  777. NodeType *node = new_op("for", OPR_FOREACH, 3);
  778. node->unode.opr.op[0] = var;
  779. node->unode.opr.op[1] = var_list;
  780. node->unode.opr.op[2] = instr;
  781. node->line_number = currentGoomSL->num_lines;
  782. return node;
  783. }
  784. static void commit_foreach(NodeType *node)
  785. {
  786. NodeType *cur = node->unode.opr.op[1];
  787. char tmp_func[256], tmp_loop[256];
  788. int lbl = allocateLabel();
  789. sprintf(tmp_func, "|foreach_func_%d|", lbl);
  790. sprintf(tmp_loop, "|foreach_loop_%d|", lbl);
  791. GSL_PUT_JUMP(tmp_loop, node->line_number);
  792. GSL_PUT_LABEL(tmp_func, node->line_number);
  793. precommit_node(node->unode.opr.op[2]);
  794. commit_node(node->unode.opr.op[2], 0);
  795. currentGoomSL->instr = gsl_instr_init(currentGoomSL, "ret", INSTR_RET, 1, node->line_number);
  796. gsl_instr_add_param(currentGoomSL->instr, "|dummy|", TYPE_LABEL);
  797. #ifdef VERBOSE
  798. printf("ret\n");
  799. #endif
  800. GSL_PUT_LABEL(tmp_loop, node->line_number);
  801. while (cur != NULL)
  802. {
  803. NodeType *x, *var;
  804. /* 1: x=var */
  805. x = nodeClone(node->unode.opr.op[0]);
  806. var = nodeClone(cur->unode.opr.op[0]);
  807. commit_node(new_set(x, var),0);
  808. /* 2: instr */
  809. currentGoomSL->instr = gsl_instr_init(currentGoomSL, "call", INSTR_CALL, 1, node->line_number);
  810. gsl_instr_add_param(currentGoomSL->instr, tmp_func, TYPE_LABEL);
  811. #ifdef VERBOSE
  812. printf("call %s\n", tmp_func);
  813. #endif
  814. /* 3: var=x */
  815. x = nodeClone(node->unode.opr.op[0]);
  816. var = cur->unode.opr.op[0];
  817. commit_node(new_set(var, x),0);
  818. cur = cur->unode.opr.op[1];
  819. }
  820. nodeFree(node->unode.opr.op[0]);
  821. } /* }}} */
  822. /* IF */
  823. static NodeType *new_if(NodeType *expression, NodeType *instr) { /* {{{ */
  824. NodeType *node = new_op("if", OPR_IF, 2);
  825. node->unode.opr.op[0] = expression;
  826. node->unode.opr.op[1] = instr;
  827. return node;
  828. }
  829. static void commit_if(NodeType *node) {
  830. char slab[1024];
  831. sprintf(slab, "|eif%d|", allocateLabel());
  832. commit_node(node->unode.opr.op[0],0);
  833. GSL_PUT_JZERO(slab,node->line_number);
  834. /* code */
  835. commit_node(node->unode.opr.op[1],0);
  836. GSL_PUT_LABEL(slab,node->line_number);
  837. } /* }}} */
  838. /* BLOCK */
  839. static NodeType *new_block(NodeType *lastNode) { /* {{{ */
  840. NodeType *blk = new_op("block", OPR_BLOCK, 2);
  841. blk->unode.opr.op[0] = new_nop("start_of_block");
  842. blk->unode.opr.op[1] = lastNode;
  843. return blk;
  844. }
  845. static void commit_block(NodeType *node) {
  846. commit_node(node->unode.opr.op[0]->unode.opr.next,0);
  847. } /* }}} */
  848. /* FUNCTION INTRO */
  849. static NodeType *new_function_intro(const char *name) { /* {{{ */
  850. char stmp[256];
  851. if (strlen(name) < 200) {
  852. sprintf(stmp, "|__func_%s|", name);
  853. }
  854. return new_op(stmp, OPR_FUNC_INTRO, 0);
  855. }
  856. static void commit_function_intro(NodeType *node) {
  857. currentGoomSL->instr = gsl_instr_init(currentGoomSL, "label", INSTR_LABEL, 1, node->line_number);
  858. gsl_instr_add_param(currentGoomSL->instr, node->str, TYPE_LABEL);
  859. #ifdef VERBOSE
  860. printf("label %s\n", node->str);
  861. #endif
  862. } /* }}} */
  863. /* FUNCTION OUTRO */
  864. static NodeType *new_function_outro() { /* {{{ */
  865. return new_op("ret", OPR_FUNC_OUTRO, 0);
  866. }
  867. static void commit_function_outro(NodeType *node) {
  868. currentGoomSL->instr = gsl_instr_init(currentGoomSL, "ret", INSTR_RET, 1, node->line_number);
  869. gsl_instr_add_param(currentGoomSL->instr, "|dummy|", TYPE_LABEL);
  870. releaseAllTemps();
  871. #ifdef VERBOSE
  872. printf("ret\n");
  873. #endif
  874. } /* }}} */
  875. /* AFFECTATION LIST */
  876. static NodeType *new_affec_list(NodeType *set, NodeType *next) /* {{{ */
  877. {
  878. NodeType *node = new_op("affect_list", OPR_AFFECT_LIST, 2);
  879. node->unode.opr.op[0] = set;
  880. node->unode.opr.op[1] = next;
  881. return node;
  882. }
  883. static NodeType *new_affect_list_after(NodeType *affect_list)
  884. {
  885. NodeType *ret = NULL;
  886. NodeType *cur = affect_list;
  887. while(cur != NULL) {
  888. NodeType *set = cur->unode.opr.op[0];
  889. NodeType *next = cur->unode.opr.op[1];
  890. NodeType *lvalue = set->unode.opr.op[0];
  891. NodeType *expression = set->unode.opr.op[1];
  892. if ((lvalue->str[0] == '&') && (expression->type == VAR_NODE)) {
  893. NodeType *nset = new_set(nodeClone(expression), nodeClone(lvalue));
  894. ret = new_affec_list(nset, ret);
  895. }
  896. cur = next;
  897. }
  898. return ret;
  899. }
  900. static void commit_affect_list(NodeType *node)
  901. {
  902. NodeType *cur = node;
  903. while(cur != NULL) {
  904. NodeType *set = cur->unode.opr.op[0];
  905. precommit_node(set->unode.opr.op[0]);
  906. precommit_node(set->unode.opr.op[1]);
  907. cur = cur->unode.opr.op[1];
  908. }
  909. cur = node;
  910. while(cur != NULL) {
  911. NodeType *set = cur->unode.opr.op[0];
  912. commit_node(set,0);
  913. cur = cur->unode.opr.op[1];
  914. }
  915. } /* }}} */
  916. /* VAR LIST */
  917. static NodeType *new_var_list(NodeType *var, NodeType *next) /* {{{ */
  918. {
  919. NodeType *node = new_op("var_list", OPR_VAR_LIST, 2);
  920. node->unode.opr.op[0] = var;
  921. node->unode.opr.op[1] = next;
  922. return node;
  923. }
  924. static void commit_var_list(NodeType *node)
  925. {
  926. } /* }}} */
  927. /* FUNCTION CALL */
  928. static NodeType *new_call(const char *name, NodeType *affect_list) { /* {{{ */
  929. HashValue *fval;
  930. fval = goom_hash_get(currentGoomSL->functions, name);
  931. if (!fval) {
  932. gsl_declare_task(name);
  933. fval = goom_hash_get(currentGoomSL->functions, name);
  934. }
  935. if (!fval) {
  936. fprintf(stderr, "ERROR: Line %d, Could not find function %s\n", currentGoomSL->num_lines, name);
  937. exit(1);
  938. return NULL;
  939. }
  940. else {
  941. ExternalFunctionStruct *gef = (ExternalFunctionStruct*)fval->ptr;
  942. if (gef->is_extern) {
  943. NodeType *node = new_op(name, OPR_EXT_CALL, 1);
  944. node->unode.opr.op[0] = affect_list;
  945. return node;
  946. }
  947. else {
  948. NodeType *node;
  949. char stmp[256];
  950. if (strlen(name) < 200) {
  951. sprintf(stmp, "|__func_%s|", name);
  952. }
  953. node = new_op(stmp, OPR_CALL, 1);
  954. node->unode.opr.op[0] = affect_list;
  955. return node;
  956. }
  957. }
  958. }
  959. static void commit_ext_call(NodeType *node) {
  960. NodeType *alafter = new_affect_list_after(node->unode.opr.op[0]);
  961. commit_node(node->unode.opr.op[0],0);
  962. currentGoomSL->instr = gsl_instr_init(currentGoomSL, "extcall", INSTR_EXT_CALL, 1, node->line_number);
  963. gsl_instr_add_param(currentGoomSL->instr, node->str, TYPE_VAR);
  964. #ifdef VERBOSE
  965. printf("extcall %s\n", node->str);
  966. #endif
  967. commit_node(alafter,0);
  968. }
  969. static void commit_call(NodeType *node) {
  970. NodeType *alafter = new_affect_list_after(node->unode.opr.op[0]);
  971. commit_node(node->unode.opr.op[0],0);
  972. currentGoomSL->instr = gsl_instr_init(currentGoomSL, "call", INSTR_CALL, 1, node->line_number);
  973. gsl_instr_add_param(currentGoomSL->instr, node->str, TYPE_LABEL);
  974. #ifdef VERBOSE
  975. printf("call %s\n", node->str);
  976. #endif
  977. commit_node(alafter,0);
  978. } /* }}} */
  979. /** **/
  980. static NodeType *rootNode = 0; /* TODO: reinitialiser a chaque compilation. */
  981. static NodeType *lastNode = 0;
  982. static NodeType *gsl_append(NodeType *curNode) {
  983. if (curNode == 0) return 0; /* {{{ */
  984. if (lastNode)
  985. lastNode->unode.opr.next = curNode;
  986. lastNode = curNode;
  987. while(lastNode->unode.opr.next) lastNode = lastNode->unode.opr.next;
  988. if (rootNode == 0)
  989. rootNode = curNode;
  990. return curNode;
  991. } /* }}} */
  992. #if 1
  993. int allocateTemp() {
  994. return allocateLabel();
  995. }
  996. void releaseAllTemps() {}
  997. void releaseTemp(int n) {}
  998. #else
  999. static int nbTemp = 0;
  1000. static int *tempArray = 0;
  1001. static int tempArraySize = 0;
  1002. int allocateTemp() { /* TODO: allocateITemp, allocateFTemp */
  1003. int i = 0; /* {{{ */
  1004. if (tempArray == 0) {
  1005. tempArraySize = 256;
  1006. tempArray = (int*)malloc(tempArraySize * sizeof(int));
  1007. }
  1008. while (1) {
  1009. int j;
  1010. for (j=0;j<nbTemp;++j) {
  1011. if (tempArray[j] == i) break;
  1012. }
  1013. if (j == nbTemp) {
  1014. if (nbTemp == tempArraySize) {
  1015. tempArraySize *= 2;
  1016. tempArray = (int*)realloc(tempArray,tempArraySize * sizeof(int));
  1017. }
  1018. tempArray[nbTemp++] = i;
  1019. return i;
  1020. }
  1021. i++;
  1022. }
  1023. } /* }}} */
  1024. void releaseAllTemps() {
  1025. nbTemp = 0; /* {{{ */
  1026. } /* }}} */
  1027. void releaseTemp(int n) {
  1028. int j; /* {{{ */
  1029. for (j=0;j<nbTemp;++j) {
  1030. if (tempArray[j] == n) {
  1031. tempArray[j] = tempArray[--nbTemp];
  1032. break;
  1033. }
  1034. }
  1035. } /* }}} */
  1036. #endif
  1037. static int lastLabel = 0;
  1038. int allocateLabel() {
  1039. return ++lastLabel; /* {{{ */
  1040. } /* }}} */
  1041. void gsl_commit_compilation()
  1042. { /* {{{ */
  1043. commit_node(rootNode,0);
  1044. rootNode = 0;
  1045. lastNode = 0;
  1046. } /* }}} */
  1047. void precommit_node(NodeType *node)
  1048. { /* {{{ */
  1049. /* do here stuff for expression.. for exemple */
  1050. if (node->type == OPR_NODE)
  1051. switch(node->unode.opr.type) {
  1052. case OPR_ADD: precommit_add(node); break;
  1053. case OPR_SUB: precommit_sub(node); break;
  1054. case OPR_MUL: precommit_mul(node); break;
  1055. case OPR_DIV: precommit_div(node); break;
  1056. case OPR_CALL_EXPR: precommit_call_expr(node); break;
  1057. }
  1058. } /* }}} */
  1059. void commit_node(NodeType *node, int releaseIfTmp)
  1060. { /* {{{ */
  1061. if (node == 0) return;
  1062. switch(node->type) {
  1063. case OPR_NODE:
  1064. switch(node->unode.opr.type) {
  1065. case OPR_SET: commit_set(node); break;
  1066. case OPR_PLUS_EQ: commit_plus_eq(node); break;
  1067. case OPR_SUB_EQ: commit_sub_eq(node); break;
  1068. case OPR_MUL_EQ: commit_mul_eq(node); break;
  1069. case OPR_DIV_EQ: commit_div_eq(node); break;
  1070. case OPR_IF: commit_if(node); break;
  1071. case OPR_WHILE: commit_while(node); break;
  1072. case OPR_BLOCK: commit_block(node); break;
  1073. case OPR_FUNC_INTRO: commit_function_intro(node); break;
  1074. case OPR_FUNC_OUTRO: commit_function_outro(node); break;
  1075. case OPR_CALL: commit_call(node); break;
  1076. case OPR_EXT_CALL: commit_ext_call(node); break;
  1077. case OPR_EQU: commit_equ(node); break;
  1078. case OPR_LOW: commit_low(node); break;
  1079. case OPR_NOT: commit_not(node); break;
  1080. case OPR_AFFECT_LIST: commit_affect_list(node); break;
  1081. case OPR_FOREACH: commit_foreach(node); break;
  1082. case OPR_VAR_LIST: commit_var_list(node); break;
  1083. #ifdef VERBOSE
  1084. case EMPTY_NODE: printf("NOP\n"); break;
  1085. #endif
  1086. }
  1087. commit_node(node->unode.opr.next,0); /* recursive for the moment, maybe better to do something iterative? */
  1088. break;
  1089. case VAR_NODE: gsl_instr_set_namespace(currentGoomSL->instr, node->vnamespace);
  1090. gsl_instr_add_param(currentGoomSL->instr, node->str, TYPE_VAR); break;
  1091. case CONST_INT_NODE: gsl_instr_add_param(currentGoomSL->instr, node->str, TYPE_INTEGER); break;
  1092. case CONST_FLOAT_NODE: gsl_instr_add_param(currentGoomSL->instr, node->str, TYPE_FLOAT); break;
  1093. case CONST_PTR_NODE: gsl_instr_add_param(currentGoomSL->instr, node->str, TYPE_PTR); break;
  1094. }
  1095. if (releaseIfTmp && is_tmp_expr(node))
  1096. releaseTemp(get_tmp_id(node));
  1097. nodeFree(node);
  1098. } /* }}} */
  1099. NodeType *nodeNew(const char *str, int type, int line_number) {
  1100. NodeType *node = (NodeType*)malloc(sizeof(NodeType)); /* {{{ */
  1101. node->type = type;
  1102. node->str = (char*)malloc(strlen(str)+1);
  1103. node->vnamespace = NULL;
  1104. node->line_number = line_number;
  1105. strcpy(node->str, str);
  1106. return node;
  1107. } /* }}} */
  1108. static NodeType *nodeClone(NodeType *node) {
  1109. NodeType *ret = nodeNew(node->str, node->type, node->line_number); /* {{{ */
  1110. ret->vnamespace = node->vnamespace;
  1111. ret->unode = node->unode;
  1112. return ret;
  1113. } /* }}} */
  1114. void nodeFreeInternals(NodeType *node) {
  1115. free(node->str); /* {{{ */
  1116. } /* }}} */
  1117. void nodeFree(NodeType *node) {
  1118. nodeFreeInternals(node); /* {{{ */
  1119. free(node);
  1120. } /* }}} */
  1121. NodeType *new_constInt(const char *str, int line_number) {
  1122. NodeType *node = nodeNew(str, CONST_INT_NODE, line_number); /* {{{ */
  1123. node->unode.constInt.val = atoi(str);
  1124. return node;
  1125. } /* }}} */
  1126. NodeType *new_constPtr(const char *str, int line_number) {
  1127. NodeType *node = nodeNew(str, CONST_PTR_NODE, line_number); /* {{{ */
  1128. node->unode.constPtr.id = strtol(str,NULL,0);
  1129. return node;
  1130. } /* }}} */
  1131. NodeType *new_constFloat(const char *str, int line_number) {
  1132. NodeType *node = nodeNew(str, CONST_FLOAT_NODE, line_number); /* {{{ */
  1133. node->unode.constFloat.val = atof(str);
  1134. return node;
  1135. } /* }}} */
  1136. NodeType *new_var(const char *str, int line_number) {
  1137. NodeType *node = nodeNew(str, VAR_NODE, line_number); /* {{{ */
  1138. node->vnamespace = gsl_find_namespace(str);
  1139. if (node->vnamespace == 0) {
  1140. fprintf(stderr, "ERROR: Line %d, Variable not found: '%s'\n", line_number, str);
  1141. exit(1);
  1142. }
  1143. return node;
  1144. } /* }}} */
  1145. NodeType *new_nop(const char *str) {
  1146. NodeType *node = new_op(str, EMPTY_NODE, 0); /* {{{ */
  1147. return node;
  1148. } /* }}} */
  1149. NodeType *new_op(const char *str, int type, int nbOp) {
  1150. int i; /* {{{ */
  1151. NodeType *node = nodeNew(str, OPR_NODE, currentGoomSL->num_lines);
  1152. node->unode.opr.next = 0;
  1153. node->unode.opr.type = type;
  1154. node->unode.opr.nbOp = nbOp;
  1155. for (i=0;i<nbOp;++i) node->unode.opr.op[i] = 0;
  1156. return node;
  1157. } /* }}} */
  1158. void gsl_declare_global_variable(int type, char *name) {
  1159. switch(type){
  1160. case -1: break;
  1161. case FLOAT_TK:gsl_float_decl_global(name);break;
  1162. case INT_TK: gsl_int_decl_global(name);break;
  1163. case PTR_TK: gsl_ptr_decl_global(name);break;
  1164. default:
  1165. {
  1166. int id = type - 1000;
  1167. gsl_struct_decl_global_from_id(name,id);
  1168. }
  1169. }
  1170. }
  1171. /* Enabling traces. */
  1172. #ifndef YYDEBUG
  1173. # define YYDEBUG 0
  1174. #endif
  1175. /* Enabling verbose error messages. */
  1176. #ifdef YYERROR_VERBOSE
  1177. # undef YYERROR_VERBOSE
  1178. # define YYERROR_VERBOSE 1
  1179. #else
  1180. # define YYERROR_VERBOSE 0
  1181. #endif
  1182. #if ! defined (YYSTYPE) && ! defined (YYSTYPE_IS_DECLARED)
  1183. #line 1199 "goomsl_yacc.y"
  1184. typedef union YYSTYPE {
  1185. int intValue;
  1186. float floatValue;
  1187. char charValue;
  1188. char strValue[2048];
  1189. NodeType *nPtr;
  1190. GoomHash *namespace;
  1191. GSL_Struct *gsl_struct;
  1192. GSL_StructField *gsl_struct_field;
  1193. } YYSTYPE;
  1194. /* Line 191 of yacc.c. */
  1195. #line 1327 "goomsl_yacc.c"
  1196. # define yystype YYSTYPE /* obsolescent; will be withdrawn */
  1197. # define YYSTYPE_IS_DECLARED 1
  1198. # define YYSTYPE_IS_TRIVIAL 1
  1199. #endif
  1200. /* Copy the second part of user declarations. */
  1201. /* Line 214 of yacc.c. */
  1202. #line 1339 "goomsl_yacc.c"
  1203. #if ! defined (yyoverflow) || YYERROR_VERBOSE
  1204. /* The parser invokes alloca or malloc; define the necessary symbols. */
  1205. # if YYSTACK_USE_ALLOCA
  1206. # define YYSTACK_ALLOC alloca
  1207. # else
  1208. # ifndef YYSTACK_USE_ALLOCA
  1209. # if defined (alloca) || defined (_ALLOCA_H)
  1210. # define YYSTACK_ALLOC alloca
  1211. # else
  1212. # ifdef __GNUC__
  1213. # define YYSTACK_ALLOC __builtin_alloca
  1214. # endif
  1215. # endif
  1216. # endif
  1217. # endif
  1218. # ifdef YYSTACK_ALLOC
  1219. /* Pacify GCC's `empty if-body' warning. */
  1220. # define YYSTACK_FREE(Ptr) do { /* empty */; } while (0)
  1221. # else
  1222. # if defined (__STDC__) || defined (__cplusplus)
  1223. # include <stdlib.h> /* INFRINGES ON USER NAME SPACE */
  1224. # define YYSIZE_T size_t
  1225. # endif
  1226. # define YYSTACK_ALLOC malloc
  1227. # define YYSTACK_FREE free
  1228. # endif
  1229. #endif /* ! defined (yyoverflow) || YYERROR_VERBOSE */
  1230. #if (! defined (yyoverflow) \
  1231. && (! defined (__cplusplus) \
  1232. || (YYSTYPE_IS_TRIVIAL)))
  1233. /* A type that is properly aligned for any stack member. */
  1234. union yyalloc
  1235. {
  1236. short yyss;
  1237. YYSTYPE yyvs;
  1238. };
  1239. /* The size of the maximum gap between one aligned stack and the next. */
  1240. # define YYSTACK_GAP_MAXIMUM (sizeof (union yyalloc) - 1)
  1241. /* The size of an array large to enough to hold all stacks, each with
  1242. N elements. */
  1243. # define YYSTACK_BYTES(N) \
  1244. ((N) * (sizeof (short) + sizeof (YYSTYPE)) \
  1245. + YYSTACK_GAP_MAXIMUM)
  1246. /* Copy COUNT objects from FROM to TO. The source and destination do
  1247. not overlap. */
  1248. # ifndef YYCOPY
  1249. # if 1 < __GNUC__
  1250. # define YYCOPY(To, From, Count) \
  1251. __builtin_memcpy (To, From, (Count) * sizeof (*(From)))
  1252. # else
  1253. # define YYCOPY(To, From, Count) \
  1254. do \
  1255. { \
  1256. register YYSIZE_T yyi; \
  1257. for (yyi = 0; yyi < (Count); yyi++) \
  1258. (To)[yyi] = (From)[yyi]; \
  1259. } \
  1260. while (0)
  1261. # endif
  1262. # endif
  1263. /* Relocate STACK from its old location to the new one. The
  1264. local variables YYSIZE and YYSTACKSIZE give the old and new number of
  1265. elements in the stack, and YYPTR gives the new location of the
  1266. stack. Advance YYPTR to a properly aligned location for the next
  1267. stack. */
  1268. # define YYSTACK_RELOCATE(Stack) \
  1269. do \
  1270. { \
  1271. YYSIZE_T yynewbytes; \
  1272. YYCOPY (&yyptr->Stack, Stack, yysize); \
  1273. Stack = &yyptr->Stack; \
  1274. yynewbytes = yystacksize * sizeof (*Stack) + YYSTACK_GAP_MAXIMUM; \
  1275. yyptr += yynewbytes / sizeof (*yyptr); \
  1276. } \
  1277. while (0)
  1278. #endif
  1279. #if defined (__STDC__) || defined (__cplusplus)
  1280. typedef signed char yysigned_char;
  1281. #else
  1282. typedef short yysigned_char;
  1283. #endif
  1284. /* YYFINAL -- State number of the termination state. */
  1285. #define YYFINAL 3
  1286. /* YYLAST -- Last index in YYTABLE. */
  1287. #define YYLAST 229
  1288. /* YYNTOKENS -- Number of terminals. */
  1289. #define YYNTOKENS 42
  1290. /* YYNNTS -- Number of nonterminals. */
  1291. #define YYNNTS 30
  1292. /* YYNRULES -- Number of rules. */
  1293. #define YYNRULES 89
  1294. /* YYNRULES -- Number of states. */
  1295. #define YYNSTATES 217
  1296. /* YYTRANSLATE(YYLEX) -- Bison symbol number corresponding to YYLEX. */
  1297. #define YYUNDEFTOK 2
  1298. #define YYMAXUTOK 279
  1299. #define YYTRANSLATE(YYX) \
  1300. ((unsigned int) (YYX) <= YYMAXUTOK ? yytranslate[YYX] : YYUNDEFTOK)
  1301. /* YYTRANSLATE[YYLEX] -- Bison symbol number corresponding to YYLEX. */
  1302. static const unsigned char yytranslate[] =
  1303. {
  1304. 0, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  1305. 25, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  1306. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  1307. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  1308. 35, 36, 32, 29, 34, 30, 2, 31, 2, 2,
  1309. 2, 2, 2, 2, 2, 2, 2, 2, 33, 2,
  1310. 27, 26, 28, 37, 2, 2, 2, 2, 2, 2,
  1311. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  1312. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  1313. 2, 40, 2, 41, 2, 2, 2, 2, 2, 2,
  1314. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  1315. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  1316. 2, 2, 2, 38, 2, 39, 2, 2, 2, 2,
  1317. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  1318. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  1319. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  1320. 2,