glsl_regen.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537
  1. #include "glsl_parser.h"
  2. #include "glsl_ast.h"
  3. #include <stdio.h>
  4. #include <stdarg.h>
  5. #include <stdlib.h>
  6. static const char *token_to_str[4096] = {
  7. [CONST] = "const",
  8. [BOOL] = "bool",
  9. [FLOAT] = "float",
  10. [DOUBLE] = "double",
  11. [INT] = "int",
  12. [UINT] = "uint",
  13. [BREAK] = "break",
  14. [CONTINUE] = "continue",
  15. [DO] = "do",
  16. [ELSE] = "else",
  17. [FOR] = "for",
  18. [IF] = "if",
  19. [DISCARD] = "discard",
  20. [RETURN] = "return",
  21. [TRUE_VALUE] = "true",
  22. [FALSE_VALUE] = "false",
  23. [SWITCH] = "switch",
  24. [CASE] = "case",
  25. [DEFAULT] = "default",
  26. [SUBROUTINE] = "subroutine",
  27. [BVEC2] = "bvec2",
  28. [BVEC3] = "bvec3",
  29. [BVEC4] = "bvec4",
  30. [IVEC2] = "ivec2",
  31. [IVEC3] = "ivec3",
  32. [IVEC4] = "ivec4",
  33. [UVEC2] = "uvec2",
  34. [UVEC3] = "uvec3",
  35. [UVEC4] = "uvec4",
  36. [VEC2] = "vec2",
  37. [VEC3] = "vec3",
  38. [VEC4] = "vec4",
  39. [MAT2] = "mat2",
  40. [MAT3] = "mat3",
  41. [MAT4] = "mat4",
  42. [CENTROID] = "centroid",
  43. [IN] = "in",
  44. [OUT] = "out",
  45. [INOUT] = "inout",
  46. [UNIFORM] = "uniform",
  47. [PATCH] = "patch",
  48. [SAMPLE] = "sample",
  49. [BUFFER] = "buffer",
  50. [SHARED] = "shared",
  51. [COHERENT] = "coherent",
  52. [VOLATILE] = "volatile",
  53. [RESTRICT] = "restrict",
  54. [READONLY] = "readonly",
  55. [WRITEONLY] = "writeonly",
  56. [DVEC2] = "dvec2",
  57. [DVEC3] = "dvec3",
  58. [DVEC4] = "dvec4",
  59. [DMAT2] = "dmat2",
  60. [DMAT3] = "dmat3",
  61. [DMAT4] = "dmat4",
  62. [NOPERSPECTIVE] = "noperspective",
  63. [FLAT] = "flat",
  64. [SMOOTH] = "smooth",
  65. [LAYOUT] = "layout",
  66. [MAT2X2] = "mat2x2",
  67. [MAT2X3] = "mat2x3",
  68. [MAT2X4] = "mat2x4",
  69. [MAT3X2] = "mat3x2",
  70. [MAT3X3] = "mat3x3",
  71. [MAT3X4] = "mat3x4",
  72. [MAT4X2] = "mat4x2",
  73. [MAT4X3] = "mat4x3",
  74. [MAT4X4] = "mat4x4",
  75. [DMAT2X2] = "dmat2x2",
  76. [DMAT2X3] = "dmat2x3",
  77. [DMAT2X4] = "dmat2x4",
  78. [DMAT3X2] = "dmat3x2",
  79. [DMAT3X3] = "dmat3x3",
  80. [DMAT3X4] = "dmat3x4",
  81. [DMAT4X2] = "dmat4x2",
  82. [DMAT4X3] = "dmat4x3",
  83. [DMAT4X4] = "dmat4x4",
  84. [ATOMIC_UINT] = "atomic_uint",
  85. [SAMPLER1D] = "sampler1D",
  86. [SAMPLER2D] = "sampler2D",
  87. [SAMPLER3D] = "sampler3D",
  88. [SAMPLERCUBE] = "samplercube",
  89. [SAMPLER1DSHADOW] = "sampler1Dshadow",
  90. [SAMPLER2DSHADOW] = "sampler2Dshadow",
  91. [SAMPLERCUBESHADOW] = "samplercubeshadow",
  92. [SAMPLER1DARRAY] = "sampler1Darray",
  93. [SAMPLER2DARRAY] = "sampler2Darray",
  94. [SAMPLER1DARRAYSHADOW] = "sampler1Darrayshadow",
  95. [SAMPLER2DARRAYSHADOW] = "sampler2Darrayshadow",
  96. [ISAMPLER1D] = "isampler1D",
  97. [ISAMPLER2D] = "isampler2D",
  98. [ISAMPLER3D] = "isampler3D",
  99. [ISAMPLERCUBE] = "isamplercube",
  100. [ISAMPLER1DARRAY] = "isampler1Darray",
  101. [ISAMPLER2DARRAY] = "isampler2Darray",
  102. [USAMPLER1D] = "usampler1D",
  103. [USAMPLER2D] = "usampler2D",
  104. [USAMPLER3D] = "usampler3D",
  105. [USAMPLERCUBE] = "usamplercube",
  106. [USAMPLER1DARRAY] = "usampler1Darray",
  107. [USAMPLER2DARRAY] = "usampler2Darray",
  108. [SAMPLER2DRECT] = "sampler2Drect",
  109. [SAMPLER2DRECTSHADOW] = "sampler2Drectshadow",
  110. [ISAMPLER2DRECT] = "isampler2Drect",
  111. [USAMPLER2DRECT] = "usampler2Drect",
  112. [SAMPLERBUFFER] = "samplerbuffer",
  113. [ISAMPLERBUFFER] = "isamplerbuffer",
  114. [USAMPLERBUFFER] = "usamplerbuffer",
  115. [SAMPLERCUBEARRAY] = "samplercubearray",
  116. [SAMPLERCUBEARRAYSHADOW] = "samplercubearrayshadow",
  117. [ISAMPLERCUBEARRAY] = "isamplercubearray",
  118. [USAMPLERCUBEARRAY] = "usamplercubearray",
  119. [SAMPLER2DMS] = "sampler2Dms",
  120. [ISAMPLER2DMS] = "isampler2Dms",
  121. [USAMPLER2DMS] = "usampler2Dms",
  122. [SAMPLER2DMSARRAY] = "sampler2Dmsarray",
  123. [ISAMPLER2DMSARRAY] = "isampler2Dmsarray",
  124. [USAMPLER2DMSARRAY] = "usampler2Dmsarray",
  125. [IMAGE1D] = "image1D",
  126. [IIMAGE1D] = "iimage1D",
  127. [UIMAGE1D] = "uimage1D",
  128. [IMAGE2D] = "image2D",
  129. [IIMAGE2D] = "iimage2D",
  130. [UIMAGE2D] = "uimage2D",
  131. [IMAGE3D] = "image3D",
  132. [IIMAGE3D] = "iimage3D",
  133. [UIMAGE3D] = "uimage3D",
  134. [IMAGE2DRECT] = "image2Drect",
  135. [IIMAGE2DRECT] = "iimage2Drect",
  136. [UIMAGE2DRECT] = "uimage2Drect",
  137. [IMAGECUBE] = "imagecube",
  138. [IIMAGECUBE] = "iimagecube",
  139. [UIMAGECUBE] = "uimagecube",
  140. [IMAGEBUFFER] = "imagebuffer",
  141. [IIMAGEBUFFER] = "iimagebuffer",
  142. [UIMAGEBUFFER] = "uimagebuffer",
  143. [IMAGE1DARRAY] = "image1Darray",
  144. [IIMAGE1DARRAY] = "iimage1Darray",
  145. [UIMAGE1DARRAY] = "uimage1Darray",
  146. [IMAGE2DARRAY] = "image2Darray",
  147. [IIMAGE2DARRAY] = "iimage2Darray",
  148. [UIMAGE2DARRAY] = "uimage2Darray",
  149. [IMAGECUBEARRAY] = "imagecubearray",
  150. [IIMAGECUBEARRAY] = "iimagecubearray",
  151. [UIMAGECUBEARRAY] = "uimagecubearray",
  152. [IMAGE2DMS] = "image2Dms",
  153. [IIMAGE2DMS] = "iimage2Dms",
  154. [UIMAGE2DMS] = "uimage2Dms",
  155. [IMAGE2DMSARRAY] = "image2Dmsarray",
  156. [IIMAGE2DMSARRAY] = "iimage2Dmsarray",
  157. [UIMAGE2DMSARRAY] = "uimage2Dmsarray",
  158. [STRUCT] = "struct",
  159. [VOID] = "void",
  160. [WHILE] = "while",
  161. [LEFT_OP] = "<<",
  162. [RIGHT_OP] = ">>",
  163. [PRE_INC_OP] = "++",
  164. [PRE_DEC_OP] = "--",
  165. [POST_INC_OP] = "++",
  166. [POST_DEC_OP] = "--",
  167. [LE_OP] = "<=",
  168. [GE_OP] = ">=",
  169. [EQ_OP] = "==",
  170. [NE_OP] = "!=",
  171. [AND_OP] = "&&",
  172. [OR_OP] = "||",
  173. [XOR_OP] = "^^",
  174. [MUL_ASSIGN] = "*=",
  175. [DIV_ASSIGN] = "/=",
  176. [ADD_ASSIGN] = "+=",
  177. [MOD_ASSIGN] = "%=",
  178. [LEFT_ASSIGN] = "<<=",
  179. [RIGHT_ASSIGN] = ">>=",
  180. [AND_ASSIGN] = "&=",
  181. [XOR_ASSIGN] = "^=",
  182. [OR_ASSIGN] = "|=",
  183. [SUB_ASSIGN] = "-=",
  184. [LEFT_PAREN] = "(",
  185. [RIGHT_PAREN] = ")",
  186. [LEFT_BRACKET] = "[",
  187. [RIGHT_BRACKET] = "]",
  188. [LEFT_BRACE] = "{",
  189. [RIGHT_BRACE] = "}",
  190. [DOT] = ".",
  191. [COMMA] = ",",
  192. [COLON] = ":",
  193. [EQUAL] = "=",
  194. [SEMICOLON] = ";",
  195. [BANG] = "!",
  196. [DASH] = "-",
  197. [TILDE] = "~",
  198. [PLUS] = "+",
  199. [STAR] = "*",
  200. [SLASH] = "/",
  201. [PERCENT] = "%",
  202. [LEFT_ANGLE] = "<",
  203. [RIGHT_ANGLE] = ">",
  204. [VERTICAL_BAR] = "|",
  205. [CARET] = "^",
  206. [AMPERSAND] = "&",
  207. [QUESTION] = "?",
  208. [INVARIANT] = "invariant",
  209. [PRECISE] = "precise",
  210. [HIGHP] = "highp",
  211. [MEDIUMP] = "mediump",
  212. [LOWP] = "lowp",
  213. [PRECISION] = "precision",
  214. [UNARY_PLUS] = "+",
  215. [UNARY_DASH] = "-",
  216. [SECTION] = "section",
  217. [NUM_GLSL_TOKEN] = ""
  218. };
  219. bool is_optional_list(struct glsl_node *n)
  220. {
  221. switch(n->code) {
  222. case ARRAY_SPECIFIER_LIST:
  223. case TYPE_QUALIFIER_LIST:
  224. return true;
  225. }
  226. return false;
  227. }
  228. struct string {
  229. char *s;
  230. int len;
  231. int capacity;
  232. };
  233. static void _glsl_regen_tree(struct glsl_node *n, struct string *out, int depth);
  234. static void string_cat(struct string *str, const char *format, ...)
  235. {
  236. int n;
  237. va_list vl;
  238. do {
  239. int left = str->capacity - str->len;
  240. va_start(vl, format);
  241. n = vsnprintf(str->s + str->len, left, format, vl);
  242. va_end(vl);
  243. if (n < left) {
  244. break;
  245. } else {
  246. str->capacity *= 2;
  247. str->s = realloc(str->s, str->capacity);
  248. }
  249. } while (1);
  250. str->len += n;
  251. }
  252. static void print_list_as_glsl(struct glsl_node *n, const char *prefix, const char *delim, const char *postfix, struct string *out, int depth)
  253. {
  254. int i, c = 0;
  255. string_cat(out,"%s", prefix);
  256. for (i = 0; i < n->child_count; i++) {
  257. if (!n->children[i]->child_count && is_optional_list(n->children[i]))
  258. continue;
  259. if (c)
  260. string_cat(out,"%s", delim);
  261. c++;
  262. _glsl_regen_tree(n->children[i], out, depth);
  263. }
  264. string_cat(out,"%s", postfix);
  265. }
  266. static void _glsl_regen_tree(struct glsl_node *n, struct string *out, int depth)
  267. {
  268. int i;
  269. int j;
  270. switch(n->code) {
  271. case IDENTIFIER:
  272. if (n->data.str)
  273. string_cat(out,"%s", n->data.str);
  274. break;
  275. case FLOATCONSTANT:
  276. string_cat(out,"%f", n->data.f);
  277. break;
  278. case DOUBLECONSTANT:
  279. string_cat(out,"%f", n->data.d);
  280. break;
  281. case INTCONSTANT:
  282. string_cat(out,"%d", n->data.i);
  283. break;
  284. case UINTCONSTANT:
  285. string_cat(out,"%u", n->data.ui);
  286. break;
  287. case TRANSLATION_UNIT:
  288. print_list_as_glsl(n, "", "\n", "\n", out, depth);
  289. break;
  290. case ARRAY_SPECIFIER_LIST:
  291. print_list_as_glsl(n, "", "", "", out, depth);
  292. break;
  293. case ARRAY_SPECIFIER:
  294. print_list_as_glsl(n, "[", "", "]", out, depth);
  295. break;
  296. case EQUAL:
  297. case MUL_ASSIGN:
  298. case DIV_ASSIGN:
  299. case MOD_ASSIGN:
  300. case ADD_ASSIGN:
  301. case SUB_ASSIGN:
  302. case LEFT_ASSIGN:
  303. case RIGHT_ASSIGN:
  304. case AND_ASSIGN:
  305. case XOR_ASSIGN:
  306. case OR_ASSIGN:
  307. case PLUS:
  308. case DASH:
  309. case STAR:
  310. case SLASH:
  311. case PERCENT:
  312. case AMPERSAND:
  313. case EQ_OP:
  314. case NE_OP:
  315. case LEFT_ANGLE:
  316. case RIGHT_ANGLE:
  317. case LE_OP:
  318. case GE_OP:
  319. case LEFT_OP:
  320. case RIGHT_OP:
  321. case CARET:
  322. case VERTICAL_BAR:
  323. case AND_OP:
  324. case OR_OP:
  325. case XOR_OP:
  326. _glsl_regen_tree(n->children[0], out, depth);
  327. if (token_to_str[n->code]) {
  328. string_cat(out," %s ", token_to_str[n->code]);
  329. } else {
  330. string_cat(out," <unknown operator %d> ", n->code);
  331. }
  332. _glsl_regen_tree(n->children[1], out, depth);
  333. break;
  334. case DOT:
  335. _glsl_regen_tree(n->children[0], out, depth);
  336. string_cat(out,".");
  337. _glsl_regen_tree(n->children[1], out, depth);
  338. break;
  339. case PRE_INC_OP:
  340. case PRE_DEC_OP:
  341. case UNARY_PLUS:
  342. case UNARY_DASH:
  343. case TILDE:
  344. case BANG:
  345. print_list_as_glsl(n, token_to_str[n->code], "", "", out, depth);
  346. break;
  347. case PAREN_EXPRESSION:
  348. print_list_as_glsl(n, "(", "", ")", out, depth);
  349. break;
  350. case POST_INC_OP:
  351. case POST_DEC_OP:
  352. print_list_as_glsl(n, "", "", token_to_str[n->code], out, depth);
  353. break;
  354. case FUNCTION_DECLARATION:
  355. case FUNCTION_HEADER:
  356. case FULLY_SPECIFIED_TYPE:
  357. case PARAMETER_DECLARATION:
  358. case PARAMETER_DECLARATOR:
  359. case TYPE_QUALIFIER_LIST:
  360. print_list_as_glsl(n, "", " ", "", out, depth);
  361. break;
  362. case FUNCTION_DEFINITION:
  363. print_list_as_glsl(n, "", " ", "\n", out, depth);
  364. break;
  365. case FUNCTION_CALL:
  366. _glsl_regen_tree(n->children[0], out, depth);
  367. _glsl_regen_tree(n->children[1], out, depth);
  368. break;
  369. case SELECTION_STATEMENT:
  370. string_cat(out,"if (");
  371. _glsl_regen_tree(n->children[0], out, depth);
  372. string_cat(out,") ");
  373. _glsl_regen_tree(n->children[1], out, depth);
  374. break;
  375. case ARRAY_REF_OP:
  376. _glsl_regen_tree(n->children[0], out, depth);
  377. string_cat(out,"[");
  378. _glsl_regen_tree(n->children[1], out, depth);
  379. string_cat(out,"]");
  380. break;
  381. case RETURN:
  382. string_cat(out,"return;\n");
  383. break;
  384. case RETURN_VALUE:
  385. string_cat(out,"return ");
  386. _glsl_regen_tree(n->children[0], out, depth);
  387. string_cat(out,";\n");
  388. break;
  389. case SELECTION_STATEMENT_ELSE:
  390. string_cat(out,"if (");
  391. _glsl_regen_tree(n->children[0], out, depth);
  392. string_cat(out,") ");
  393. _glsl_regen_tree(n->children[1], out, depth);
  394. string_cat(out," else ");
  395. _glsl_regen_tree(n->children[2], out, depth);
  396. string_cat(out,"\n");
  397. break;
  398. case SINGLE_DECLARATION:
  399. print_list_as_glsl(n, "", " ", ";", out, depth);
  400. break;
  401. case SINGLE_INIT_DECLARATION:
  402. _glsl_regen_tree(n->children[0], out, depth);
  403. string_cat(out," ");
  404. _glsl_regen_tree(n->children[1], out, depth);
  405. string_cat(out," = ");
  406. _glsl_regen_tree(n->children[3], out, depth);
  407. string_cat(out,";");
  408. break;
  409. case WHILE_STATEMENT:
  410. string_cat(out,"while (");
  411. _glsl_regen_tree(n->children[0], out, depth);
  412. string_cat(out,") ");
  413. _glsl_regen_tree(n->children[1], out, depth);
  414. break;
  415. case DO_STATEMENT:
  416. string_cat(out,"do ");
  417. _glsl_regen_tree(n->children[0], out, depth);
  418. string_cat(out," while ( ");
  419. _glsl_regen_tree(n->children[1], out, depth);
  420. string_cat(out," );");
  421. break;
  422. case FOR_STATEMENT:
  423. string_cat(out,"for (");
  424. _glsl_regen_tree(n->children[0], out, depth);
  425. string_cat(out," ");
  426. _glsl_regen_tree(n->children[1], out, depth);
  427. string_cat(out,") ");
  428. _glsl_regen_tree(n->children[2], out, depth);
  429. break;
  430. case ASSIGNMENT_CONDITION:
  431. _glsl_regen_tree(n->children[0], out, depth);
  432. string_cat(out," ");
  433. _glsl_regen_tree(n->children[1], out, depth);
  434. string_cat(out," = ");
  435. _glsl_regen_tree(n->children[2], out, depth);
  436. break;
  437. case STATEMENT_LIST:
  438. string_cat(out,"{\n");
  439. for (i = 0; i < n->child_count; i++) {
  440. for (j = 0; j < depth + 1; j++) string_cat(out,"\t");
  441. _glsl_regen_tree(n->children[i], out, depth + 1);
  442. string_cat(out,"\n");
  443. }
  444. for (j = 0; j < depth; j++) string_cat(out,"\t");
  445. string_cat(out,"}");
  446. break;
  447. case STRUCT_DECLARATION_LIST:
  448. for (i = 0; i < n->child_count; i++) {
  449. for (j = 0; j < depth + 1; j++) string_cat(out,"\t");
  450. _glsl_regen_tree(n->children[i], out, depth + 1);
  451. string_cat(out,"\n");
  452. }
  453. for (j = 0; j < depth; j++) string_cat(out,"\t");
  454. break;
  455. case BLOCK_DECLARATION:
  456. _glsl_regen_tree(n->children[0], out, depth);
  457. string_cat(out," ");
  458. _glsl_regen_tree(n->children[1], out, depth);
  459. string_cat(out," {\n");
  460. _glsl_regen_tree(n->children[2], out, depth);
  461. string_cat(out,"} ");
  462. _glsl_regen_tree(n->children[3], out, depth);
  463. if (n->children[4]->child_count) {
  464. string_cat(out," ");
  465. _glsl_regen_tree(n->children[4], out, depth);
  466. }
  467. string_cat(out,";");
  468. break;
  469. case BREAK:
  470. string_cat(out,"break;");
  471. break;
  472. case STRUCT_DECLARATOR:
  473. print_list_as_glsl(n, "", " ", "", out, depth);
  474. break;
  475. case STRUCT_DECLARATOR_LIST:
  476. print_list_as_glsl(n, "", ",", "", out, depth);
  477. break;
  478. case STRUCT_DECLARATION:
  479. print_list_as_glsl(n, "", " ", ";", out, depth);
  480. break;
  481. case FUNCTION_PARAMETER_LIST:
  482. case FUNCTION_CALL_PARAMETER_LIST:
  483. print_list_as_glsl(n, "(", ", ", ")", out, depth);
  484. break;
  485. case FOR_REST_STATEMENT:
  486. _glsl_regen_tree(n->children[0], out, depth);
  487. string_cat(out,"; ");
  488. if (n->child_count == 2) {
  489. _glsl_regen_tree(n->children[1], out, depth);
  490. }
  491. break;
  492. case DECLARATION:
  493. _glsl_regen_tree(n->children[0], out, depth);
  494. string_cat(out,"; ");
  495. break;
  496. case SECTION_STATEMENT:
  497. _glsl_regen_tree(n->children[1], out, depth);
  498. break;
  499. case DECLARATION_STATEMENT_LIST:
  500. print_list_as_glsl(n, "", "\n", "", out, depth);
  501. break;
  502. case TYPE_SPECIFIER:
  503. case POSTFIX_EXPRESSION:
  504. case CONDITION_OPT:
  505. case EXPRESSION_CONDITION:
  506. print_list_as_glsl(n, "", "", "", out, depth);
  507. break;
  508. case EXPRESSION_STATEMENT:
  509. print_list_as_glsl(n, "", "", ";", out, depth);
  510. break;
  511. default:
  512. if (token_to_str[n->code])
  513. string_cat(out,"%s", token_to_str[n->code]);
  514. else
  515. string_cat(out,"<unknown token %d>", n->code);
  516. break;
  517. }
  518. }
  519. char *glsl_regen_tree(struct glsl_node *n)
  520. {
  521. struct string s;
  522. s.s = malloc(1024);
  523. s.len = 0;
  524. s.capacity = 1024;
  525. _glsl_regen_tree(n, &s, 0);
  526. return s.s;
  527. }