1 /*
2 	expr.h
3 
4 	expression construction and manipulations
5 
6 	Copyright (C) 2001 Bill Currie <bill@taniwha.org>
7 
8 	Author: Bill Currie <bill@taniwha.org>
9 	Date: 2001/06/15
10 
11 	This program is free software; you can redistribute it and/or
12 	modify it under the terms of the GNU General Public License
13 	as published by the Free Software Foundation; either version 2
14 	of the License, or (at your option) any later version.
15 
16 	This program is distributed in the hope that it will be useful,
17 	but WITHOUT ANY WARRANTY; without even the implied warranty of
18 	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
19 
20 	See the GNU General Public License for more details.
21 
22 	You should have received a copy of the GNU General Public License
23 	along with this program; if not, write to:
24 
25 		Free Software Foundation, Inc.
26 		59 Temple Place - Suite 330
27 		Boston, MA  02111-1307, USA
28 
29 */
30 
31 #ifndef __expr_h
32 #define __expr_h
33 
34 #include "QF/pr_comp.h"
35 
36 /**	\defgroup qfcc_expr Expressions
37 	\ingroup qfcc
38 */
39 //@{
40 
41 /**	Type of the exression node in an expression tree.
42 */
43 typedef enum {
44 	ex_error,		///< error expression. used to signal an error
45 	ex_state,		///< state expression (::ex_state_t)
46 	ex_bool,		///< short circuit boolean logic expression (::ex_bool_t)
47 	ex_label,		///< goto/branch label (::ex_label_t)
48 	ex_labelref,	///< label reference (::ex_labelref_t)
49 	ex_block,		///< statement block expression (::ex_block_t)
50 	ex_expr,		///< binary expression (::ex_expr_t)
51 	ex_uexpr,		///< unary expression (::ex_expr_t)
52 	ex_symbol,		///< non-temporary variable (::symbol_t)
53 	ex_temp,		///< temporary variable (::ex_temp_t)
54 
55 	ex_nil,			///< umm, nil, null. nuff said (0 of any type)
56 	ex_value,		///< constant value (::ex_value_t)
57 } expr_type;
58 
59 /**	Binary and unary expressions.
60 
61 	This is used for both binary and unary expressions. Unary expressions do
62 	not use e2. The opcode is generally the parser token for the expression,
63 	though special codes are used for non-math expressions.
64 */
65 typedef struct ex_expr_s {
66 	int            op;		///< op-code of this expression
67 	struct type_s *type;	///< the type of the result of this expression
68 	struct expr_s *e1;		///< left side of binary, sole of unary
69 	struct expr_s *e2;		///< right side of binary, null for unary
70 } ex_expr_t;
71 
72 typedef struct ex_label_s {
73 	struct ex_label_s *next;
74 	struct reloc_s *refs;		///< relocations associated with this label
75 	struct sblock_s *dest;		///< the location of this label if known
76 	const char *name;			///< the name of this label
77 	int         used;			///< label is used as a target
78 	struct daglabel_s *daglabel;
79 } ex_label_t;
80 
81 typedef struct {
82 	ex_label_t *label;
83 } ex_labelref_t;
84 
85 typedef struct {
86 	struct expr_s *head;	///< the first expression in the block
87 	struct expr_s **tail;	///< last expression in the block, for appending
88 	struct expr_s *result;	///< the result of this block if non-void
89 	int         is_call;	///< this block exprssion forms a function call
90 } ex_block_t;
91 
92 typedef struct {
93 	struct expr_s *expr;
94 	struct operand_s *op;	///< The operand for the temporary variable, if
95 							///< allocated
96 	struct type_s *type;	///< The type of the temporary variable.
97 } ex_temp_t;
98 
99 /**	Pointer constant expression.
100 
101 	Represent a pointer to an absolute address in data space.
102 */
103 typedef struct ex_pointer_s {
104 	int         val;
105 	struct type_s *type;
106 	struct def_s *def;
107 } ex_pointer_t;
108 
109 typedef struct ex_func_s {
110 	int         val;
111 	struct type_s *type;
112 } ex_func_t;
113 
114 typedef struct {
115 	int         size;
116 	struct expr_s *e[1];
117 } ex_list_t;
118 
119 typedef struct {
120 	ex_list_t  *true_list;
121 	ex_list_t  *false_list;
122 	struct expr_s *e;
123 } ex_bool_t;
124 
125 /**	State expression used for think function state-machines.
126 
127 	State expressions are of the form <code>[framenum, nextthink]</code>
128 	(standard) or <code>[framenum, nextthink, timestep]</code> (QF extension)
129 	and come before the opening brace of the function. If the state
130 	expression is of the former form, then \c step will be null. Normally,
131 	\c framenum and \c nextthink must be constant (though \c nextthink may
132 	be a forward reference), but qfcc allows both \c framenum and
133 	\c nextthink, and also \c timestep, to be variable.
134 
135 	\par From qcc:
136 		States are special functions made for convenience.  They
137 		automatically set frame, nextthink (implicitly), and think (allowing
138 		forward definitions).
139 
140 	\verbatim
141 	void() name = [framenum, nextthink] {code};
142 	\endverbatim
143 	expands to:
144 	\verbatim
145 	void name ()
146 	{
147 	     self.frame=framenum;
148 	     self.nextthink = time + 0.1;
149 	     self.think = nextthink
150 	     [code]
151 	};
152 	\endverbatim
153 
154 	Although the above expansion shows three expressions, a state expression
155 	using constant values is just one instruction: either
156 	<code>state framenum, nextthink</code> (standard) or
157 	<code>state.f framenum, nextthink, timestep</code> (QF, optional).
158 */
159 typedef struct {
160 	struct expr_s *frame;		///< the frame to which to change in this state
161 	struct expr_s *think;		///< think function for the next state
162 	struct expr_s *step;		///< time step until the next state
163 } ex_state_t;
164 
165 typedef struct ex_value_s {
166 	struct ex_value_s *next;
167 	struct daglabel_s *daglabel;///< dag label for this value
168 	etype_t     type;
169 	union {
170 		const char *string_val;			///< string constant
171 		float       float_val;			///< float constant
172 		float       vector_val[3];		///< vector constant
173 		int         entity_val;			///< entity constant
174 		ex_func_t   func_val;			///< function constant
175 		ex_pointer_t pointer;			///< pointer constant
176 		float       quaternion_val[4];	///< quaternion constant
177 		int         integer_val;		///< integer constant
178 		unsigned    uinteger_val;		///< unsigned integer constant
179 		short       short_val;			///< short constant
180 	} v;
181 } ex_value_t;
182 
183 #define POINTER_VAL(p) (((p).def ? (p).def->offset : 0) + (p).val)
184 
185 typedef struct expr_s {
186 	struct expr_s *next;		///< the next expression in a block expression
187 	expr_type	type;			///< the type of the result of this expression
188 	int			line;			///< source line that generated this expression
189 	string_t	file;			///< source file that generated this expression
190 	int         printid;		///< avoid duplicate output when printing
191 	unsigned	paren:1;		///< the expression is enclosed in ()
192 	unsigned	rvalue:1;		///< the expression is on the right side of =
193 	union {
194 		ex_label_t  label;				///< label expression
195 		ex_labelref_t labelref;			///< label reference expression (&)
196 		ex_state_t  state;				///< state expression
197 		ex_bool_t   bool;				///< boolean logic expression
198 		ex_block_t  block;				///< statement block expression
199 		ex_expr_t   expr;				///< binary or unary expression
200 		struct symbol_s *symbol;		///< symbol reference expression
201 		ex_temp_t   temp;				///< temporary variable expression
202 		ex_value_t *value;				///< constant value
203 	} e;
204 } expr_t;
205 
206 extern struct type_s *ev_types[];
207 
208 /**	Report a type mismatch error.
209 
210 	\a e1 is used for reporting the file and line number of the error.
211 
212 	\param e1		Left side expression. Used for reporting the type.
213 	\param e2		Right side expression. Used for reporting the type.
214 	\param op		The opcode of the expression.
215 	\return			\a e1 with its type set to ex_error.
216 */
217 expr_t *type_mismatch (expr_t *e1, expr_t *e2, int op);
218 
219 expr_t *param_mismatch (expr_t *e, int param, const char *fn,
220 					    struct type_s *t1, struct type_s *t2);
221 expr_t *cast_error (expr_t *e, struct type_s *t1, struct type_s *t2);
222 expr_t *test_error (expr_t *e, struct type_s *t);
223 
224 extern expr_t *local_expr;
225 
226 /**	Get the type descriptor of the expression result.
227 
228 	\param e		The expression from which to get the result type.
229 	\return         Pointer to the type description, or null if the expression
230 					type (expr_t::type) is inappropriate.
231 */
232 struct type_s *get_type (expr_t *e);
233 
234 /**	Get the basic type code of the expression result.
235 
236 	\param e		The expression from which to get the result type.
237 	\return         Pointer to the type description, or ev_type_count if
238 					get_type() returns null.
239 */
240 etype_t extract_type (expr_t *e);
241 
242 /**	Create a new expression node.
243 
244 	Sets the source file and line number information. The expression node is
245 	otherwise raw. This function is generally not used directly.
246 
247 	\return			The new expression node.
248 */
249 expr_t *new_expr (void);
250 
251 /**	Create a deep copy of an expression tree.
252 
253 	\param e		The root of the expression tree to copy.
254 	\return			A new expression tree giving the same expression.
255 */
256 expr_t *copy_expr (expr_t *e);
257 
258 /**	Create a new label name.
259 
260 	The label name is guaranteed to to the compilation. It is made up of the
261 	name of the current function plus an incrementing number. The number is
262 	not reset between functions.
263 
264 	\return			The string representing the label name.
265 */
266 const char *new_label_name (void);
267 
268 /**	Create a new label expression node.
269 
270 	The label name is set using new_label_name().
271 
272 	\return			The new label expression (::ex_label_t) node.
273 */
274 expr_t *new_label_expr (void);
275 
276 /**	Create a new label reference expression node.
277 
278 	Used for taking the address of a label (eg. jump tables).
279 	The label's \a used field is incremented.
280 
281 	\return			The new label reference expression (::ex_labelref_t) node.
282 */
283 expr_t *new_label_ref (ex_label_t *label);
284 
285 /**	Create a new state expression node.
286 
287 	The label name is set using new_label_name(), and the label is linked
288 	into the global list of labels for later resolution.
289 
290 	\param frame	The expression giving the frame number.
291 	\param think	The expression giving the think function.
292 	\param step		The expression giving the time step value, or null if
293 					no time-step is specified (standard form).
294 	\return			The new state expression (::ex_state_t) node.
295 */
296 expr_t *new_state_expr (expr_t *frame, expr_t *think, expr_t *step);
297 
298 expr_t *new_bool_expr (ex_list_t *true_list, ex_list_t *false_list, expr_t *e);
299 
300 /**	Create a new statement block expression node.
301 
302 	The returned block expression is empty.  Use append_expr() to add
303 	expressions to the block expression.
304 
305 	\return			The new block expression (::ex_block_t) node.
306 */
307 expr_t *new_block_expr (void);
308 
309 /**	Create a new binary expression node node.
310 
311 	If either \a e1 or \a e2 are error expressions, then that expression will
312 	be returned instead of a new binary expression.
313 
314 	\param op		The op-ccode of the binary expression.
315 	\param e1		The left side of the binary expression.
316 	\param e2		The right side of the binary expression.
317 	\return			The new binary expression node (::ex_expr_t) if neither
318 					\a e1 nor \a e2 are error expressions, otherwise the
319 					expression that is an error expression.
320 */
321 expr_t *new_binary_expr (int op, expr_t *e1, expr_t *e2);
322 
323 /**	Create a new unary expression node node.
324 
325 	If \a e1 is an error expression, then it will be returned instead of a
326 	new binary expression.
327 
328 	\param op		The op-code of the unary expression.
329 	\param e1		The "right" side of the expression.
330 	\return			The new unary expression node (::ex_expr_t) if \a e1
331 					is not an error expression, otherwise \a e1.
332 */
333 expr_t *new_unary_expr (int op, expr_t *e1);
334 
335 /**	Create a new symbol reference (non-temporary variable) expression node.
336 
337 	\return 		The new symbol reference expression node (::symbol_t).
338 */
339 expr_t *new_symbol_expr (struct symbol_s *symbol);
340 
341 /**	Create a new temporary variable expression node.
342 
343 	Does not allocate a new temporary variable.
344 	The ex_temp_t::users field will be 0.
345 
346 	\param type		The type of the temporary variable.
347 	\return			The new temporary variable expression node (ex_temp_t).
348 */
349 expr_t *new_temp_def_expr (struct type_s *type);
350 
351 /**	Create a new nil expression node.
352 
353 	nil represents 0 of any type.
354 
355 	\return			The new nil expression node.
356 */
357 expr_t *new_nil_expr (void);
358 
359 /** Create a new value expression node.
360 
361 	\param value	The value to put in the expression node.
362 	\return			The new value expression.
363 */
364 expr_t *new_value_expr (ex_value_t *value);
365 
366 /**	Create a new symbol expression node from a name.
367 
368 	\param name		The name for the symbol.
369 	\return			The new symbol expression.
370 */
371 expr_t *new_name_expr (const char *name);
372 
373 /** Create a new string constant expression node.
374 
375 	\param string_val	The string constant being represented.
376 	\return			The new string constant expression node
377 					(expr_t::e::string_val).
378 */
379 expr_t *new_string_expr (const char *string_val);
380 const char *expr_string (expr_t *e);
381 
382 /** Create a new float constant expression node.
383 
384 	\param float_val	The float constant being represented.
385 	\return			The new float constant expression node
386 					(expr_t::e::float_val).
387 */
388 expr_t *new_float_expr (float float_val);
389 float expr_float (expr_t *e);
390 
391 /** Create a new vector constant expression node.
392 
393 	\param vector_val	The vector constant being represented.
394 	\return			The new vector constant expression node
395 					(expr_t::e::vector_val).
396 */
397 expr_t *new_vector_expr (const float *vector_val);
398 const float *expr_vector (expr_t *e);
399 
400 /** Create a new entity constant expression node.
401 
402 	\param entity_val	The entity constant being represented.
403 	\return			The new entity constant expression node
404 					(expr_t::e::entity_val).
405 */
406 expr_t *new_entity_expr (int entity_val);
407 
408 /** Create a new field constant expression node.
409 
410 	\param field_val	XXX
411 	\param type		The type of the field.
412 	\param def
413 	\return			The new field constant expression node
414 					(expr_t::e::field_val).
415 */
416 expr_t *new_field_expr (int field_val, struct type_s *type, struct def_s *def);
417 
418 /** Create a new function constant expression node.
419 
420 	\param func_val	The function constant being represented.
421 	\param type		The type of the function
422 	\return			The new function constant expression node
423 					(expr_t::e::func_val).
424 */
425 expr_t *new_func_expr (int func_val, struct type_s *type);
426 
427 /** Create a new pointer constant expression node.
428 
429 	\param val		The pointer constant (address) being represented. XXX
430 	\param type		The type of the referenced value.
431 	\param def
432 	\return			The new pointer constant expression node
433 					(expr_t::e::pointer_val).
434 */
435 expr_t *new_pointer_expr (int val, struct type_s *type, struct def_s *def);
436 
437 /** Create a new quaternion constant expression node.
438 
439 	\param quaternion_val	The quaternion constant being represented.
440 	\return			The new quaternion constant expression node
441 					(expr_t::e::quaternion_val).
442 */
443 expr_t *new_quaternion_expr (const float *quaternion_val);
444 const float *expr_quaternion (expr_t *e);
445 
446 /** Create a new integer constant expression node.
447 
448 	\param integer_val	The integer constant being represented.
449 	\return			The new integer constant expression node
450 					(expr_t::e::integer_val).
451 */
452 expr_t *new_integer_expr (int integer_val);
453 int expr_integer (expr_t *e);
454 
455 /** Create a new integer constant expression node.
456 
457 	\param uinteger_val	The integer constant being represented.
458 	\return			The new integer constant expression node
459 					(expr_t::e::integer_val).
460 */
461 expr_t *new_uinteger_expr (unsigned uinteger_val);
462 unsigned expr_uinteger (expr_t *e);
463 
464 /** Create a new short constant expression node.
465 
466 	\param short_val	The short constant being represented.
467 	\return			The new short constant expression node
468 					(expr_t::e::short_val).
469 */
470 expr_t *new_short_expr (short short_val);
471 short expr_short (expr_t *e);
472 
473 /**	Check of the expression refers to a constant value.
474 
475 	\param e		The expression to check.
476 	\return			True if the expression is constant.
477 */
478 int is_constant (expr_t *e);
479 
480 /**	Return a value expression representing the constant stored in \a e.
481 
482 	If \a e does not represent a constant, or \a e is already a value or
483 	nil expression, then \a e is returned rather than a new expression.
484 
485 	\param e		The expression from which to extract the value.
486 	\return			A new expression holding the value of \a e or \e itself.
487 */
488 expr_t *constant_expr (expr_t *e);
489 
490 /**	Check if the op-code is a comparison.
491 
492 	\param op		The op-code to check.
493 	\return			True if the op-code is a comparison operator.
494 */
495 int is_compare (int op);
496 
497 /**	Check if the op-code is a math operator.
498 
499 	\param op		The op-code to check.
500 	\return			True if the op-code is a math operator.
501 */
502 int is_math_op (int op);
503 
504 /**	Check if the op-code is a logic operator.
505 
506 	\param op		The op-code to check.
507 	\return			True if the op-code is a logic operator.
508 */
509 int is_logic (int op);
510 
511 int is_string_val (expr_t *e);
512 int is_float_val (expr_t *e);
513 int is_vector_val (expr_t *e);
514 int is_quaternion_val (expr_t *e);
515 int is_integer_val (expr_t *e);
516 int is_short_val (expr_t *e);
517 
518 /**	Create a reference to the global <code>.self</code> entity variable.
519 
520 	This is used for <code>\@self</code>.
521 	\return			A new expression referencing the <code>.self</code> def.
522 */
523 expr_t *new_self_expr (void);
524 
525 /**	Create a reference to the <code>.this</code> entity field.
526 
527 	This is used for <code>\@this</code>.
528 	\return			A new expression referencing the <code>.this</code> def.
529 */
530 expr_t *new_this_expr (void);
531 
532 /**	Create an expression of the correct type that references the return slot.
533 
534 	\param type		The type of the reference to the return slot.
535 	\return			A new expression referencing the return slot.
536 */
537 expr_t *new_ret_expr (struct type_s *type);
538 
539 expr_t *new_alias_expr (struct type_s *type, expr_t *expr);
540 
541 /**	Create an expression of the correct type that references the specified
542 	parameter slot.
543 
544 	\param type		The type of the reference to the parameter slot.
545 	\param num		The index of the parameter (0-7).
546 	\return			A new expression referencing the parameter slot.
547 */
548 expr_t *new_param_expr (struct type_s *type, int num);
549 
550 /**	Create an expression representing a block copy.
551 
552 	This is used for structure assignments.
553 
554 	\param e1		Destination of move.
555 	\param e2		Source of move.
556 	\param type		type giving size of move.
557 	\param indirect	Move uses dereferenced pointers.
558 	\return			A new expression representing the move.
559 */
560 expr_t *new_move_expr (expr_t *e1, expr_t *e2, struct type_s *type,
561 					   int indirect);
562 
563 /**	Convert a name to an expression of the appropriate type.
564 
565 	Converts the expression in-place. If the exprssion is not a name
566 	expression (ex_name), no converision takes place.
567 
568 	\param e		The expression to convert.
569 */
570 void convert_name (expr_t *e);
571 
572 expr_t *append_expr (expr_t *block, expr_t *e);
573 
574 void print_expr (expr_t *e);
575 void dump_dot_expr (void *e, const char *filename);
576 
577 void convert_int (expr_t *e);
578 void convert_short (expr_t *e);
579 void convert_short_int (expr_t *e);
580 void convert_nil (expr_t *e, struct type_s *t);
581 
582 expr_t *test_expr (expr_t *e);
583 void backpatch (ex_list_t *list, expr_t *label);
584 expr_t *convert_bool (expr_t *e, int block);
585 expr_t *bool_expr (int op, expr_t *label, expr_t *e1, expr_t *e2);
586 expr_t *binary_expr (int op, expr_t *e1, expr_t *e2);
587 expr_t *asx_expr (int op, expr_t *e1, expr_t *e2);
588 expr_t *unary_expr (int op, expr_t *e);
589 expr_t *build_function_call (expr_t *fexpr, struct type_s *ftype,
590 							 expr_t *params);
591 expr_t *function_expr (expr_t *e1, expr_t *e2);
592 struct function_s;
593 expr_t *branch_expr (int op, expr_t *test, expr_t *label);
594 expr_t *goto_expr (expr_t *label);
595 expr_t *return_expr (struct function_s *f, expr_t *e);
596 expr_t *conditional_expr (expr_t *cond, expr_t *e1, expr_t *e2);
597 expr_t *incop_expr (int op, expr_t *e, int postop);
598 expr_t *array_expr (expr_t *array, expr_t *index);
599 expr_t *pointer_expr (expr_t *pointer);
600 expr_t *address_expr (expr_t *e1, expr_t *e2, struct type_s *t);
601 expr_t *build_if_statement (expr_t *test, expr_t *s1, expr_t *els, expr_t *s2);
602 expr_t *build_while_statement (expr_t *test, expr_t *statement,
603 							   expr_t *break_label, expr_t *continue_label);
604 expr_t *build_do_while_statement (expr_t *statement, expr_t *test,
605 								  expr_t *break_label, expr_t *continue_label);
606 expr_t *build_for_statement (expr_t *init, expr_t *test, expr_t *next,
607 							 expr_t *statement,
608 							 expr_t *break_label, expr_t *continue_label);
609 expr_t *build_state_expr (expr_t *frame, expr_t *think, expr_t *step);
610 expr_t *think_expr (struct symbol_s *think_sym);
611 expr_t *assign_expr (expr_t *e1, expr_t *e2);
612 expr_t *cast_expr (struct type_s *t, expr_t *e);
613 
614 const char *get_op_string (int op);
615 
616 struct keywordarg_s;
617 struct class_type_s;
618 expr_t *selector_expr (struct keywordarg_s *selector);
619 expr_t *protocol_expr (const char *protocol);
620 expr_t *encode_expr (struct type_s *type);
621 expr_t *super_expr (struct class_type_s *class_type);
622 expr_t *message_expr (expr_t *receiver, struct keywordarg_s *message);
623 expr_t *sizeof_expr (expr_t *expr, struct type_s *type);
624 
625 expr_t *fold_constants (expr_t *e);
626 
627 //@}
628 
629 #endif//__expr_h
630