Clone of mesa.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ast.h 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626
  1. /* -*- c++ -*- */
  2. /*
  3. * Copyright © 2009 Intel Corporation
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a
  6. * copy of this software and associated documentation files (the "Software"),
  7. * to deal in the Software without restriction, including without limitation
  8. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  9. * and/or sell copies of the Software, and to permit persons to whom the
  10. * Software is furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice (including the next
  13. * paragraph) shall be included in all copies or substantial portions of the
  14. * Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  19. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  21. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  22. * DEALINGS IN THE SOFTWARE.
  23. */
  24. #pragma once
  25. #ifndef AST_H
  26. #define AST_H
  27. #include "main/simple_list.h"
  28. #include "list.h"
  29. #include "glsl_parser_extras.h"
  30. struct ir_instruction;
  31. struct _mesa_glsl_parse_state;
  32. struct YYLTYPE;
  33. class ast_node : public simple_node {
  34. public:
  35. virtual ~ast_node();
  36. virtual void print(void) const;
  37. virtual ir_rvalue *hir(exec_list *instructions,
  38. struct _mesa_glsl_parse_state *state);
  39. /**
  40. * Retrieve the source location of an AST node
  41. *
  42. * This function is primarily used to get the source position of an AST node
  43. * into a form that can be passed to \c _mesa_glsl_error.
  44. *
  45. * \sa _mesa_glsl_error, ast_node::set_location
  46. */
  47. struct YYLTYPE get_location(void) const
  48. {
  49. struct YYLTYPE locp;
  50. locp.source = this->location.source;
  51. locp.first_line = this->location.line;
  52. locp.first_column = this->location.column;
  53. locp.last_line = locp.first_line;
  54. locp.last_column = locp.first_column;
  55. return locp;
  56. }
  57. /**
  58. * Set the source location of an AST node from a parser location
  59. *
  60. * \sa ast_node::get_location
  61. */
  62. void set_location(const struct YYLTYPE &locp)
  63. {
  64. this->location.source = locp.source;
  65. this->location.line = locp.first_line;
  66. this->location.column = locp.first_column;
  67. }
  68. struct {
  69. unsigned source;
  70. unsigned line;
  71. unsigned column;
  72. } location;
  73. protected:
  74. ast_node(void);
  75. };
  76. enum ast_operators {
  77. ast_assign,
  78. ast_plus, /**< Unary + operator. */
  79. ast_neg,
  80. ast_add,
  81. ast_sub,
  82. ast_mul,
  83. ast_div,
  84. ast_mod,
  85. ast_lshift,
  86. ast_rshift,
  87. ast_less,
  88. ast_greater,
  89. ast_lequal,
  90. ast_gequal,
  91. ast_equal,
  92. ast_nequal,
  93. ast_bit_and,
  94. ast_bit_xor,
  95. ast_bit_or,
  96. ast_bit_not,
  97. ast_logic_and,
  98. ast_logic_xor,
  99. ast_logic_or,
  100. ast_logic_not,
  101. ast_mul_assign,
  102. ast_div_assign,
  103. ast_mod_assign,
  104. ast_add_assign,
  105. ast_sub_assign,
  106. ast_ls_assign,
  107. ast_rs_assign,
  108. ast_and_assign,
  109. ast_xor_assign,
  110. ast_or_assign,
  111. ast_conditional,
  112. ast_pre_inc,
  113. ast_pre_dec,
  114. ast_post_inc,
  115. ast_post_dec,
  116. ast_field_selection,
  117. ast_array_index,
  118. ast_function_call,
  119. ast_identifier,
  120. ast_int_constant,
  121. ast_uint_constant,
  122. ast_float_constant,
  123. ast_bool_constant,
  124. ast_sequence
  125. };
  126. class ast_expression : public ast_node {
  127. public:
  128. ast_expression(int oper, ast_expression *,
  129. ast_expression *, ast_expression *);
  130. ast_expression(const char *identifier) :
  131. oper(ast_identifier)
  132. {
  133. subexpressions[0] = NULL;
  134. subexpressions[1] = NULL;
  135. subexpressions[2] = NULL;
  136. primary_expression.identifier = (char *) identifier;
  137. }
  138. static const char *operator_string(enum ast_operators op);
  139. virtual ir_rvalue *hir(exec_list *instructions,
  140. struct _mesa_glsl_parse_state *state);
  141. virtual void print(void) const;
  142. enum ast_operators oper;
  143. ast_expression *subexpressions[3];
  144. union {
  145. char *identifier;
  146. int int_constant;
  147. float float_constant;
  148. unsigned uint_constant;
  149. int bool_constant;
  150. } primary_expression;
  151. /**
  152. * List of expressions for an \c ast_sequence.
  153. */
  154. struct simple_node expressions;
  155. };
  156. class ast_expression_bin : public ast_expression {
  157. public:
  158. ast_expression_bin(int oper, ast_expression *, ast_expression *);
  159. virtual void print(void) const;
  160. };
  161. /**
  162. * Subclass of expressions for function calls
  163. */
  164. class ast_function_expression : public ast_expression {
  165. public:
  166. ast_function_expression(ast_expression *callee)
  167. : ast_expression(ast_function_call, callee,
  168. NULL, NULL),
  169. cons(false)
  170. {
  171. /* empty */
  172. }
  173. ast_function_expression(class ast_type_specifier *type)
  174. : ast_expression(ast_function_call, (ast_expression *) type,
  175. NULL, NULL),
  176. cons(true)
  177. {
  178. /* empty */
  179. }
  180. bool is_constructor() const
  181. {
  182. return cons;
  183. }
  184. virtual ir_rvalue *hir(exec_list *instructions,
  185. struct _mesa_glsl_parse_state *state);
  186. private:
  187. /**
  188. * Is this function call actually a constructor?
  189. */
  190. bool cons;
  191. };
  192. /**
  193. * Number of possible operators for an ast_expression
  194. *
  195. * This is done as a define instead of as an additional value in the enum so
  196. * that the compiler won't generate spurious messages like "warning:
  197. * enumeration value ‘ast_num_operators’ not handled in switch"
  198. */
  199. #define AST_NUM_OPERATORS (ast_sequence + 1)
  200. class ast_compound_statement : public ast_node {
  201. public:
  202. ast_compound_statement(int new_scope, ast_node *statements);
  203. virtual void print(void) const;
  204. virtual ir_rvalue *hir(exec_list *instructions,
  205. struct _mesa_glsl_parse_state *state);
  206. int new_scope;
  207. struct simple_node statements;
  208. };
  209. class ast_declaration : public ast_node {
  210. public:
  211. ast_declaration(char *identifier, int is_array, ast_expression *array_size,
  212. ast_expression *initializer);
  213. virtual void print(void) const;
  214. char *identifier;
  215. int is_array;
  216. ast_expression *array_size;
  217. ast_expression *initializer;
  218. };
  219. enum {
  220. ast_precision_high = 0, /**< Default precision. */
  221. ast_precision_medium,
  222. ast_precision_low
  223. };
  224. struct ast_type_qualifier {
  225. unsigned invariant:1;
  226. unsigned constant:1;
  227. unsigned attribute:1;
  228. unsigned varying:1;
  229. unsigned in:1;
  230. unsigned out:1;
  231. unsigned centroid:1;
  232. unsigned uniform:1;
  233. unsigned smooth:1;
  234. unsigned flat:1;
  235. unsigned noperspective:1;
  236. };
  237. class ast_struct_specifier : public ast_node {
  238. public:
  239. ast_struct_specifier(char *identifier, ast_node *declarator_list);
  240. virtual void print(void) const;
  241. char *name;
  242. struct simple_node declarations;
  243. };
  244. enum ast_types {
  245. ast_void,
  246. ast_float,
  247. ast_int,
  248. ast_uint,
  249. ast_bool,
  250. ast_vec2,
  251. ast_vec3,
  252. ast_vec4,
  253. ast_bvec2,
  254. ast_bvec3,
  255. ast_bvec4,
  256. ast_ivec2,
  257. ast_ivec3,
  258. ast_ivec4,
  259. ast_uvec2,
  260. ast_uvec3,
  261. ast_uvec4,
  262. ast_mat2,
  263. ast_mat2x3,
  264. ast_mat2x4,
  265. ast_mat3x2,
  266. ast_mat3,
  267. ast_mat3x4,
  268. ast_mat4x2,
  269. ast_mat4x3,
  270. ast_mat4,
  271. ast_sampler1d,
  272. ast_sampler2d,
  273. ast_sampler2drect,
  274. ast_sampler3d,
  275. ast_samplercube,
  276. ast_sampler1dshadow,
  277. ast_sampler2dshadow,
  278. ast_sampler2drectshadow,
  279. ast_samplercubeshadow,
  280. ast_sampler1darray,
  281. ast_sampler2darray,
  282. ast_sampler1darrayshadow,
  283. ast_sampler2darrayshadow,
  284. ast_isampler1d,
  285. ast_isampler2d,
  286. ast_isampler3d,
  287. ast_isamplercube,
  288. ast_isampler1darray,
  289. ast_isampler2darray,
  290. ast_usampler1d,
  291. ast_usampler2d,
  292. ast_usampler3d,
  293. ast_usamplercube,
  294. ast_usampler1darray,
  295. ast_usampler2darray,
  296. ast_struct,
  297. ast_type_name
  298. };
  299. class ast_type_specifier : public ast_node {
  300. public:
  301. ast_type_specifier(int specifier);
  302. /** Construct a type specifier from a type name */
  303. ast_type_specifier(const char *name)
  304. : type_specifier(ast_type_name), type_name(name), structure(NULL),
  305. is_array(false), array_size(NULL), precision(ast_precision_high)
  306. {
  307. /* empty */
  308. }
  309. /** Construct a type specifier from a structure definition */
  310. ast_type_specifier(ast_struct_specifier *s)
  311. : type_specifier(ast_struct), type_name(s->name), structure(s),
  312. is_array(false), array_size(NULL), precision(ast_precision_high)
  313. {
  314. /* empty */
  315. }
  316. const struct glsl_type *glsl_type(const char **name,
  317. struct _mesa_glsl_parse_state *state)
  318. const;
  319. virtual void print(void) const;
  320. enum ast_types type_specifier;
  321. const char *type_name;
  322. ast_struct_specifier *structure;
  323. int is_array;
  324. ast_expression *array_size;
  325. unsigned precision:2;
  326. };
  327. class ast_fully_specified_type : public ast_node {
  328. public:
  329. virtual void print(void) const;
  330. ast_type_qualifier qualifier;
  331. ast_type_specifier *specifier;
  332. };
  333. class ast_declarator_list : public ast_node {
  334. public:
  335. ast_declarator_list(ast_fully_specified_type *);
  336. virtual void print(void) const;
  337. virtual ir_rvalue *hir(exec_list *instructions,
  338. struct _mesa_glsl_parse_state *state);
  339. ast_fully_specified_type *type;
  340. struct simple_node declarations;
  341. /**
  342. * Special flag for vertex shader "invariant" declarations.
  343. *
  344. * Vertex shaders can contain "invariant" variable redeclarations that do
  345. * not include a type. For example, "invariant gl_Position;". This flag
  346. * is used to note these cases when no type is specified.
  347. */
  348. int invariant;
  349. };
  350. class ast_parameter_declarator : public ast_node {
  351. public:
  352. virtual void print(void) const;
  353. virtual ir_rvalue *hir(exec_list *instructions,
  354. struct _mesa_glsl_parse_state *state);
  355. ast_fully_specified_type *type;
  356. char *identifier;
  357. int is_array;
  358. ast_expression *array_size;
  359. static void parameters_to_hir(simple_node *ast_parameters,
  360. bool formal, exec_list *ir_parameters,
  361. struct _mesa_glsl_parse_state *state);
  362. private:
  363. /** Is this parameter declaration part of a formal parameter list? */
  364. bool formal_parameter;
  365. /**
  366. * Is this parameter 'void' type?
  367. *
  368. * This field is set by \c ::hir.
  369. */
  370. bool is_void;
  371. };
  372. class ast_function : public ast_node {
  373. public:
  374. ast_function(void);
  375. virtual void print(void) const;
  376. virtual ir_rvalue *hir(exec_list *instructions,
  377. struct _mesa_glsl_parse_state *state);
  378. ast_fully_specified_type *return_type;
  379. char *identifier;
  380. struct simple_node parameters;
  381. private:
  382. /**
  383. * Is this prototype part of the function definition?
  384. *
  385. * Used by ast_function_definition::hir to process the parameters, etc.
  386. * of the function.
  387. *
  388. * \sa ::hir
  389. */
  390. bool is_definition;
  391. /**
  392. * Function signature corresponding to this function prototype instance
  393. *
  394. * Used by ast_function_definition::hir to process the parameters, etc.
  395. * of the function.
  396. *
  397. * \sa ::hir
  398. */
  399. class ir_function_signature *signature;
  400. friend class ast_function_definition;
  401. };
  402. class ast_declaration_statement : public ast_node {
  403. public:
  404. ast_declaration_statement(void);
  405. enum {
  406. ast_function,
  407. ast_declaration,
  408. ast_precision
  409. } mode;
  410. union {
  411. class ast_function *function;
  412. ast_declarator_list *declarator;
  413. ast_type_specifier *type;
  414. ast_node *node;
  415. } declaration;
  416. };
  417. class ast_expression_statement : public ast_node {
  418. public:
  419. ast_expression_statement(ast_expression *);
  420. virtual void print(void) const;
  421. virtual ir_rvalue *hir(exec_list *instructions,
  422. struct _mesa_glsl_parse_state *state);
  423. ast_expression *expression;
  424. };
  425. class ast_case_label : public ast_node {
  426. public:
  427. /**
  428. * An expression of NULL means 'default'.
  429. */
  430. ast_expression *expression;
  431. };
  432. class ast_selection_statement : public ast_node {
  433. public:
  434. ast_selection_statement(ast_expression *condition,
  435. ast_node *then_statement,
  436. ast_node *else_statement);
  437. virtual void print(void) const;
  438. virtual ir_rvalue *hir(exec_list *instructions,
  439. struct _mesa_glsl_parse_state *state);
  440. ast_expression *condition;
  441. ast_node *then_statement;
  442. ast_node *else_statement;
  443. };
  444. class ast_switch_statement : public ast_node {
  445. public:
  446. ast_expression *expression;
  447. struct simple_node statements;
  448. };
  449. class ast_iteration_statement : public ast_node {
  450. public:
  451. ast_iteration_statement(int mode, ast_node *init, ast_node *condition,
  452. ast_expression *rest_expression, ast_node *body);
  453. virtual void print(void) const;
  454. virtual ir_rvalue *hir(exec_list *, struct _mesa_glsl_parse_state *);
  455. enum ast_iteration_modes {
  456. ast_for,
  457. ast_while,
  458. ast_do_while
  459. } mode;
  460. ast_node *init_statement;
  461. ast_node *condition;
  462. ast_expression *rest_expression;
  463. ast_node *body;
  464. private:
  465. /**
  466. * Generate IR from the condition of a loop
  467. *
  468. * This is factored out of ::hir because some loops have the condition
  469. * test at the top (for and while), and others have it at the end (do-while).
  470. */
  471. void condition_to_hir(class ir_loop *, struct _mesa_glsl_parse_state *);
  472. };
  473. class ast_jump_statement : public ast_node {
  474. public:
  475. ast_jump_statement(int mode, ast_expression *return_value);
  476. virtual void print(void) const;
  477. virtual ir_rvalue *hir(exec_list *instructions,
  478. struct _mesa_glsl_parse_state *state);
  479. enum ast_jump_modes {
  480. ast_continue,
  481. ast_break,
  482. ast_return,
  483. ast_discard
  484. } mode;
  485. ast_expression *opt_return_value;
  486. };
  487. class ast_function_definition : public ast_node {
  488. public:
  489. virtual void print(void) const;
  490. virtual ir_rvalue *hir(exec_list *instructions,
  491. struct _mesa_glsl_parse_state *state);
  492. ast_function *prototype;
  493. ast_compound_statement *body;
  494. };
  495. extern void
  496. _mesa_ast_to_hir(exec_list *instructions, struct _mesa_glsl_parse_state *state);
  497. extern struct ir_rvalue *
  498. _mesa_ast_field_selection_to_hir(const struct ast_expression *expr,
  499. exec_list *instructions,
  500. struct _mesa_glsl_parse_state *state);
  501. #endif /* AST_H */