Source Documentation

class array_symbol : public symbol
#include <symtab.hh>

Derived symbol type, used for arrays.

Study the following declaration:

var arr : array[10] of real;

The type attribute (see above) in this case is an index to the name type real. We see that we also need an attribute, #array_cardinality, which in this example would have the value 10. To be slightly more general, we also have an attribute, #index_type, which at present can only point to the name type #integer_type.

Public Functions

array_symbol(const pool_index)
inline virtual array_symbol *get_array_symbol()

Public Members

sym_index index_type

Points to the index type in the symbol table.

int array_cardinality

Note: cardinality = nr of elements,.

Protected Functions

virtual void print(ostream&)
class ast_add : public ast_binaryoperation
#include <ast.hh>

Plus node. a + b.

Example:

-a + 5 * c
AST for an addition.

Public Functions

ast_add(position_information*, ast_expression*, ast_expression*)
virtual sym_index type_check()

Perform type checking. See semantic.cc for the method bodies. Note that it’s an error to call type_check in this class. It should only be called in the concrete AST nodes, see below.

virtual void optimize()

Perform optimization. See optimize.cc for the method bodies. Just as with type checking, it’s an error to call optimize in this class. It should only be called in the concrete AST nodes.

virtual sym_index generate_quads(quad_list&)

Generate quads. See quads.cc for the method bodies. Like type checking, generate_quads should only be called in concrete AST nodes.

inline virtual ast_add *get_ast_binaryoperation()

Protected Functions

virtual void print(ostream&)
class ast_and : public ast_binaryoperation
#include <ast.hh>

Logical AND node. a AND b.

Public Functions

ast_and(position_information*, ast_expression*, ast_expression*)
virtual sym_index type_check()

Perform type checking. See semantic.cc for the method bodies. Note that it’s an error to call type_check in this class. It should only be called in the concrete AST nodes, see below.

virtual void optimize()

Perform optimization. See optimize.cc for the method bodies. Just as with type checking, it’s an error to call optimize in this class. It should only be called in the concrete AST nodes.

virtual sym_index generate_quads(quad_list&)

Generate quads. See quads.cc for the method bodies. Like type checking, generate_quads should only be called in concrete AST nodes.

inline virtual ast_and *get_ast_binaryoperation()

Protected Functions

virtual void print(ostream&)
class ast_assign : public ast_statement
#include <ast.hh>

Assignment node. a := b or a[i] = b.

Note that this class inherits ast_statement, and therefore does not return a value. Constructs of the type a := (b := 2); are not allowed in DIESEL.

Public Functions

ast_assign(position_information*, ast_lvalue*, ast_expression*)
virtual sym_index type_check()

Perform type checking. See semantic.cc for the method bodies. Note that it’s an error to call type_check in this class. It should only be called in the concrete AST nodes, see below.

virtual void optimize()

Perform optimization. See optimize.cc for the method bodies. Just as with type checking, it’s an error to call optimize in this class. It should only be called in the concrete AST nodes.

virtual sym_index generate_quads(quad_list&)

Generate quads. See quads.cc for the method bodies. Like type checking, generate_quads should only be called in concrete AST nodes.

Public Members

ast_lvalue *lhs

The left hand side (lhs), ie, the variable being assigned to.

ast_expression *rhs

The right hand side (rhs), ie, the value being assigned.

Protected Functions

virtual void print(ostream&)
class ast_binaryoperation : public ast_expression
#include <ast.hh>

Base class for all binary operation nodes. a + b, etc. Note: the left and right operands are defined here instead of in the individual subclasses, making those rather trivial.

Subclassed by ast_add, ast_and, ast_divide, ast_idiv, ast_mod, ast_mult, ast_or, ast_sub

Public Functions

ast_binaryoperation(position_information*, ast_expression*, ast_expression*)
virtual sym_index type_check()

Perform type checking. See semantic.cc for the method bodies. Note that it’s an error to call type_check in this class. It should only be called in the concrete AST nodes, see below.

virtual void optimize()

Perform optimization. See optimize.cc for the method bodies. Just as with type checking, it’s an error to call optimize in this class. It should only be called in the concrete AST nodes.

virtual sym_index generate_quads(quad_list&) = 0

Generate quads. See quads.cc for the method bodies. Like type checking, generate_quads should only be called in concrete AST nodes.

inline virtual ast_binaryoperation *get_ast_binaryoperation()

Public Members

ast_expression *left

Left child of the operation.

ast_expression *right

Right child of the operation.

Protected Functions

virtual void print(ostream&)
virtual void xprint(ostream&, string)
class ast_binaryrelation : public ast_expression
#include <ast.hh>

Base class for all binary relation nodes. a < b, etc.

Always returns a integer value, where 0 (zero) means false and everything else means true.

Note: the left and right operands are defined here instead of in the individual subclasses, making those rather trivial.

Subclassed by ast_equal, ast_greaterthan, ast_lessthan, ast_notequal

Public Functions

ast_binaryrelation(position_information*, ast_expression*, ast_expression*)
virtual sym_index type_check()

Perform type checking. See semantic.cc for the method bodies. Note that it’s an error to call type_check in this class. It should only be called in the concrete AST nodes, see below.

virtual void optimize()

Perform optimization. See optimize.cc for the method bodies. Just as with type checking, it’s an error to call optimize in this class. It should only be called in the concrete AST nodes.

virtual sym_index generate_quads(quad_list&) = 0

Generate quads. See quads.cc for the method bodies. Like type checking, generate_quads should only be called in concrete AST nodes.

Public Members

ast_expression *left

Left child of the operation.

ast_expression *right

Right child of the operation.

Protected Functions

virtual void print(ostream&)
virtual void xprint(ostream&, string)
class ast_cast : public ast_expression
#include <ast.hh>

A cast node.

This class represents type transformation, casting an integer to a real. It is inserted into the AST at appropriate places during type checking. See semantic.cc.

Public Functions

ast_cast(position_information*, ast_expression*)
virtual void optimize()

Perform optimization. See optimize.cc for the method bodies. Just as with type checking, it’s an error to call optimize in this class. It should only be called in the concrete AST nodes.

virtual sym_index generate_quads(quad_list&)

Generate quads. See quads.cc for the method bodies. Like type checking, generate_quads should only be called in concrete AST nodes.

inline virtual ast_cast *get_ast_cast()

Public Members

ast_expression *expr

The expression to cast to real.

Protected Functions

virtual void print(ostream&)
class ast_divide : public ast_binaryoperation
#include <ast.hh>

Real division node. a / b, where at least one of a and b have real type.

Public Functions

ast_divide(position_information*, ast_expression*, ast_expression*)
virtual sym_index type_check()

Perform type checking. See semantic.cc for the method bodies. Note that it’s an error to call type_check in this class. It should only be called in the concrete AST nodes, see below.

virtual void optimize()

Perform optimization. See optimize.cc for the method bodies. Just as with type checking, it’s an error to call optimize in this class. It should only be called in the concrete AST nodes.

virtual sym_index generate_quads(quad_list&)

Generate quads. See quads.cc for the method bodies. Like type checking, generate_quads should only be called in concrete AST nodes.

inline virtual ast_divide *get_ast_binaryoperation()

Protected Functions

virtual void print(ostream&)
class ast_elsif : public ast_node
#include <ast.hh>

represents an elsif clause inside an if-statement. The class is a bit special, so it inherits directly from ast_node.

Public Functions

ast_elsif(position_information*, ast_expression*, ast_stmt_list*)
virtual sym_index type_check()

Perform type checking. See semantic.cc for the method bodies. Note that it’s an error to call type_check in this class. It should only be called in the concrete AST nodes, see below.

virtual void optimize()

Perform optimization. See optimize.cc for the method bodies. Just as with type checking, it’s an error to call optimize in this class. It should only be called in the concrete AST nodes.

virtual sym_index generate_quads(quad_list&)

Generate quads. See quads.cc for the method bodies. Like type checking, generate_quads should only be called in concrete AST nodes.

virtual void generate_quads_and_jump(quad_list&, int)

Public Members

ast_expression *condition

The test condition; decides if we should execute the body.

ast_stmt_list *body

The body to execute if the condition evaluates to true.

Protected Functions

virtual void print(ostream&)
class ast_elsif_list : public ast_node
#include <ast.hh>

Contains a list of elsif clauses.

Public Functions

ast_elsif_list(position_information*, ast_elsif*)

Constructor for the first element of a list.

ast_elsif_list(position_information*, ast_elsif*, ast_elsif_list*)

Constructor to add a new elsif clause to the list.

virtual sym_index type_check()

Perform type checking. See semantic.cc for the method bodies. Note that it’s an error to call type_check in this class. It should only be called in the concrete AST nodes, see below.

virtual void optimize()

Perform optimization. See optimize.cc for the method bodies. Just as with type checking, it’s an error to call optimize in this class. It should only be called in the concrete AST nodes.

virtual sym_index generate_quads(quad_list&)

Generate quads. See quads.cc for the method bodies. Like type checking, generate_quads should only be called in concrete AST nodes.

virtual void generate_quads_and_jump(quad_list&, int)

Public Members

ast_elsif *last_elsif

Points to the last (last added) elsif clause in the list.

ast_elsif_list *preceding

Points to a list of the preceding elsif clauses. Is NULL when empty.

Protected Functions

virtual void print(ostream&)
class ast_equal : public ast_binaryrelation
#include <ast.hh>

Equality operator. a = b.

Public Functions

ast_equal(position_information*, ast_expression*, ast_expression*)
virtual sym_index type_check()

Perform type checking. See semantic.cc for the method bodies. Note that it’s an error to call type_check in this class. It should only be called in the concrete AST nodes, see below.

virtual void optimize()

Perform optimization. See optimize.cc for the method bodies. Just as with type checking, it’s an error to call optimize in this class. It should only be called in the concrete AST nodes.

virtual sym_index generate_quads(quad_list&)

Generate quads. See quads.cc for the method bodies. Like type checking, generate_quads should only be called in concrete AST nodes.

Protected Functions

virtual void print(ostream&)
class ast_expr_list : public ast_node
#include <ast.hh>

Contains a list of expressions. Currently only used for parameter lists.

Note: The parameters will be stored in reverse order! This is due to how the grammar is written, it’s easiest this way.

Public Functions

ast_expr_list(position_information*, ast_expression*)

Constructor for the first element of a list.

ast_expr_list(position_information*, ast_expression*, ast_expr_list*)

Constructor to add a new expression to the list.

virtual sym_index type_check()

Perform type checking. See semantic.cc for the method bodies. Note that it’s an error to call type_check in this class. It should only be called in the concrete AST nodes, see below.

virtual void optimize()

Perform optimization. See optimize.cc for the method bodies. Just as with type checking, it’s an error to call optimize in this class. It should only be called in the concrete AST nodes.

virtual sym_index generate_quads(quad_list&)

Generate quads. See quads.cc for the method bodies. Like type checking, generate_quads should only be called in concrete AST nodes.

virtual void generate_parameter_list(quad_list&, parameter_symbol*, int*)

Public Members

ast_expression *last_expr

Points to the last (last added) expression in the list.

ast_expr_list *preceding

Points to a list of the preceding expressions. Is NULL when empty.

Protected Functions

virtual void print(ostream&)
class ast_expression : public ast_node
#include <ast.hh>

The superclass for all DIESEL constructs that do return a value. It has a type attribute, which denotes the type of its value (real_type, integer_type, or void_type).

Subclassed by ast_binaryoperation, ast_binaryrelation, ast_cast, ast_functioncall, ast_integer, ast_lvalue, ast_not, ast_real, ast_uminus

Public Functions

ast_expression(position_information*)
ast_expression(position_information*, sym_index)
virtual sym_index type_check()

Perform type checking. See semantic.cc for the method bodies. Note that it’s an error to call type_check in this class. It should only be called in the concrete AST nodes, see below.

virtual void optimize()

Perform optimization. See optimize.cc for the method bodies. Just as with type checking, it’s an error to call optimize in this class. It should only be called in the concrete AST nodes.

virtual sym_index generate_quads(quad_list&) = 0

Generate quads. See quads.cc for the method bodies. Like type checking, generate_quads should only be called in concrete AST nodes.

inline virtual ast_integer *get_ast_integer()
inline virtual ast_real *get_ast_real()
inline virtual ast_id *get_ast_id()
inline virtual ast_cast *get_ast_cast()
inline virtual ast_binaryoperation *get_ast_binaryoperation()

Public Members

sym_index type

The return type of this expression.

Protected Functions

virtual void print(ostream&)
class ast_functioncall : public ast_expression
#include <ast.hh>

Represents a function call. a = calc(foo);

If you wonder how the return value is handled, or where the function call is actually connected to the function body… Don’t worry. It will become clear later on. Remember that we parse one block at a time, so that the call of a function takes place on one lexical level higher (as in less deeply nested) than the execution of the function body, except in certain cases involving recursive function calls.

Public Functions

ast_functioncall(position_information*, ast_id*, ast_expr_list*)
virtual sym_index type_check()

Perform type checking. See semantic.cc for the method bodies. Note that it’s an error to call type_check in this class. It should only be called in the concrete AST nodes, see below.

virtual void optimize()

Perform optimization. See optimize.cc for the method bodies. Just as with type checking, it’s an error to call optimize in this class. It should only be called in the concrete AST nodes.

virtual sym_index generate_quads(quad_list&)

Generate quads. See quads.cc for the method bodies. Like type checking, generate_quads should only be called in concrete AST nodes.

Public Members

ast_id *id

The function’s id node, contains a link to the symbol table.

ast_expr_list *parameter_list

A list of actual parameters. If no parameters, this list is NULL.

Protected Functions

virtual void print(ostream&)
class ast_functionhead : public ast_node
#include <ast.hh>

A node used to transfer information about an environment. Used in parser.y for setting the proper return type of a function, and type checking. It is never part of a function body.

Public Functions

ast_functionhead(position_information*, sym_index)
virtual void optimize()

Perform optimization. See optimize.cc for the method bodies. Just as with type checking, it’s an error to call optimize in this class. It should only be called in the concrete AST nodes.

virtual sym_index generate_quads(quad_list&)

Generate quads. See quads.cc for the method bodies. Like type checking, generate_quads should only be called in concrete AST nodes.

quad_list *do_quads(ast_stmt_list *s)

Starts the generation of a quad list for a program block pointed to by the argument. It then adds on the last label quad, and returns the list when it is done.

Parameters

s – the function body to generate quads for.

Public Members

sym_index sym_p

Pointer to the function in the symbol table.

Protected Functions

virtual void print(ostream&)
class ast_greaterthan : public ast_binaryrelation
#include <ast.hh>

Greater than operator. a > b.

Public Functions

ast_greaterthan(position_information*, ast_expression*, ast_expression*)
virtual sym_index type_check()

Perform type checking. See semantic.cc for the method bodies. Note that it’s an error to call type_check in this class. It should only be called in the concrete AST nodes, see below.

virtual void optimize()

Perform optimization. See optimize.cc for the method bodies. Just as with type checking, it’s an error to call optimize in this class. It should only be called in the concrete AST nodes.

virtual sym_index generate_quads(quad_list&)

Generate quads. See quads.cc for the method bodies. Like type checking, generate_quads should only be called in concrete AST nodes.

Protected Functions

virtual void print(ostream&)
class ast_id : public ast_lvalue
#include <ast.hh>

An identifier node. Can be the name of a variable, function, constant, etc…

Public Functions

ast_id(position_information*)
ast_id(position_information*, sym_index)
virtual sym_index type_check()

Perform type checking. See semantic.cc for the method bodies. Note that it’s an error to call type_check in this class. It should only be called in the concrete AST nodes, see below.

virtual void optimize()

Perform optimization. See optimize.cc for the method bodies. Just as with type checking, it’s an error to call optimize in this class. It should only be called in the concrete AST nodes.

virtual sym_index generate_quads(quad_list&)

Generate quads. See quads.cc for the method bodies. Like type checking, generate_quads should only be called in concrete AST nodes.

virtual void generate_assignment(quad_list&, sym_index)
inline virtual ast_id *get_ast_id()

Public Members

sym_index sym_p

A symbol table index for this symbol.

Protected Functions

virtual void print(ostream&)
class ast_idiv : public ast_binaryoperation
#include <ast.hh>

Integer division node. a div b, where both operands have integer type.

Public Functions

ast_idiv(position_information*, ast_expression*, ast_expression*)
virtual sym_index type_check()

Perform type checking. See semantic.cc for the method bodies. Note that it’s an error to call type_check in this class. It should only be called in the concrete AST nodes, see below.

virtual void optimize()

Perform optimization. See optimize.cc for the method bodies. Just as with type checking, it’s an error to call optimize in this class. It should only be called in the concrete AST nodes.

virtual sym_index generate_quads(quad_list&)

Generate quads. See quads.cc for the method bodies. Like type checking, generate_quads should only be called in concrete AST nodes.

inline virtual ast_idiv *get_ast_binaryoperation()

Protected Functions

virtual void print(ostream&)
class ast_if : public ast_statement
#include <ast.hh>

An if clause. Has lots of children. if - then - elsif - else.

Example:

if a > b then
  c := a;
else
  c := b;
end;
AST for if-statement with an else-clause.

Example:

if a > b then
  c := a
elsif a = b
  then c := 0
elsif a < b then
  c := b
else
  c := -1
end;
AST for an if-statement with elsif clauses.

Public Functions

ast_if(position_information*, ast_expression*, ast_stmt_list*, ast_elsif_list*, ast_stmt_list*)
virtual sym_index type_check()

Perform type checking. See semantic.cc for the method bodies. Note that it’s an error to call type_check in this class. It should only be called in the concrete AST nodes, see below.

virtual void optimize()

Perform optimization. See optimize.cc for the method bodies. Just as with type checking, it’s an error to call optimize in this class. It should only be called in the concrete AST nodes.

virtual sym_index generate_quads(quad_list&)

Generate quads. See quads.cc for the method bodies. Like type checking, generate_quads should only be called in concrete AST nodes.

Public Members

ast_expression *condition

The primary if-condition.

ast_stmt_list *body

The primary if-body, executed if condition evaluates to non-zero.

ast_elsif_list *elsif_list

A list of elsif clauses. May be NULL.

ast_stmt_list *else_body

The else body, executed if ‘condition’ evaluates to zero. May be NULL.

Protected Functions

virtual void print(ostream&)
class ast_indexed : public ast_lvalue
#include <ast.hh>

An array identifier node. Index must be of integer type.

Example:

a[a[1]] := a[2] + 3;
AST for an indexed assignment.

Public Functions

ast_indexed(position_information*, ast_id*, ast_expression*)
virtual sym_index type_check()

Perform type checking. See semantic.cc for the method bodies. Note that it’s an error to call type_check in this class. It should only be called in the concrete AST nodes, see below.

virtual void optimize()

Perform optimization. See optimize.cc for the method bodies. Just as with type checking, it’s an error to call optimize in this class. It should only be called in the concrete AST nodes.

virtual sym_index generate_quads(quad_list&)

Generate quads. See quads.cc for the method bodies. Like type checking, generate_quads should only be called in concrete AST nodes.

virtual void generate_assignment(quad_list&, sym_index)

Public Members

ast_id *id

The array’s id node, which contains a link into the symbol table.

ast_expression *index

The index expression. Must be of type integer.

Protected Functions

virtual void print(ostream&)
class ast_integer : public ast_expression
#include <ast.hh>

An integer node. Represents an integer number, like 5.

Public Functions

ast_integer(position_information*, long)
virtual sym_index type_check()

Perform type checking. See semantic.cc for the method bodies. Note that it’s an error to call type_check in this class. It should only be called in the concrete AST nodes, see below.

virtual void optimize()

Perform optimization. See optimize.cc for the method bodies. Just as with type checking, it’s an error to call optimize in this class. It should only be called in the concrete AST nodes.

virtual sym_index generate_quads(quad_list&)

Generate quads. See quads.cc for the method bodies. Like type checking, generate_quads should only be called in concrete AST nodes.

inline virtual ast_integer *get_ast_integer()

Public Members

long value

The integer value of the node.

Protected Functions

virtual void print(ostream&)
class ast_lessthan : public ast_binaryrelation
#include <ast.hh>

Less than operator. a < b.

Public Functions

ast_lessthan(position_information*, ast_expression*, ast_expression*)
virtual sym_index type_check()

Perform type checking. See semantic.cc for the method bodies. Note that it’s an error to call type_check in this class. It should only be called in the concrete AST nodes, see below.

virtual void optimize()

Perform optimization. See optimize.cc for the method bodies. Just as with type checking, it’s an error to call optimize in this class. It should only be called in the concrete AST nodes.

virtual sym_index generate_quads(quad_list&)

Generate quads. See quads.cc for the method bodies. Like type checking, generate_quads should only be called in concrete AST nodes.

Protected Functions

virtual void print(ostream&)
class ast_lvalue : public ast_expression
#include <ast.hh>

The superclass for all DIESEL constructs that can be assigned a value (i.e., which evaluate to a reference to a storage location). The name lvalue comes from the fact that it represents expressions that can appear on the left-hand side of an assignment (e.g., x := 2;).

Subclassed by ast_id, ast_indexed

Public Functions

ast_lvalue(position_information*)
ast_lvalue(position_information*, sym_index)
virtual sym_index type_check()

Perform type checking. See semantic.cc for the method bodies. Note that it’s an error to call type_check in this class. It should only be called in the concrete AST nodes, see below.

virtual void optimize()

Perform optimization. See optimize.cc for the method bodies. Just as with type checking, it’s an error to call optimize in this class. It should only be called in the concrete AST nodes.

virtual sym_index generate_quads(quad_list&) = 0

Generate quads. See quads.cc for the method bodies. Like type checking, generate_quads should only be called in concrete AST nodes.

virtual void generate_assignment(quad_list&, sym_index) = 0

Protected Functions

virtual void print(ostream&)
class ast_mod : public ast_binaryoperation
#include <ast.hh>

Integer mod node. a mod b, where both operands have integer type.

Public Functions

ast_mod(position_information*, ast_expression*, ast_expression*)
virtual sym_index type_check()

Perform type checking. See semantic.cc for the method bodies. Note that it’s an error to call type_check in this class. It should only be called in the concrete AST nodes, see below.

virtual void optimize()

Perform optimization. See optimize.cc for the method bodies. Just as with type checking, it’s an error to call optimize in this class. It should only be called in the concrete AST nodes.

virtual sym_index generate_quads(quad_list&)

Generate quads. See quads.cc for the method bodies. Like type checking, generate_quads should only be called in concrete AST nodes.

inline virtual ast_mod *get_ast_binaryoperation()

Protected Functions

virtual void print(ostream&)
class ast_mult : public ast_binaryoperation
#include <ast.hh>

Multiplication node. a * b.

Public Functions

ast_mult(position_information*, ast_expression*, ast_expression*)
virtual sym_index type_check()

Perform type checking. See semantic.cc for the method bodies. Note that it’s an error to call type_check in this class. It should only be called in the concrete AST nodes, see below.

virtual void optimize()

Perform optimization. See optimize.cc for the method bodies. Just as with type checking, it’s an error to call optimize in this class. It should only be called in the concrete AST nodes.

virtual sym_index generate_quads(quad_list&)

Generate quads. See quads.cc for the method bodies. Like type checking, generate_quads should only be called in concrete AST nodes.

inline virtual ast_mult *get_ast_binaryoperation()

Protected Functions

virtual void print(ostream&)
class ast_node
#include <ast.hh>

The superclass of all other AST nodes. It is essentially an empty class, holding only position information, a tag (to help identify the node type when downcasting is needed), and methods for printing AST nodes; all of which are things common to all nodes.

Subclassed by ast_elsif, ast_elsif_list, ast_expr_list, ast_expression, ast_functionhead, ast_procedurehead, ast_statement, ast_stmt_list

Public Functions

ast_node(position_information*)
virtual sym_index type_check()

Perform type checking. See semantic.cc for the method bodies. Note that it’s an error to call type_check in this class. It should only be called in the concrete AST nodes, see below.

virtual void optimize()

Perform optimization. See optimize.cc for the method bodies. Just as with type checking, it’s an error to call optimize in this class. It should only be called in the concrete AST nodes.

virtual sym_index generate_quads(quad_list&) = 0

Generate quads. See quads.cc for the method bodies. Like type checking, generate_quads should only be called in concrete AST nodes.

Public Members

position_information *pos

Holds line and column number for this node.

ast_node_type tag

Describes what kind of node this is. We need to be able to check this in a convenient way during AST optimization and C++ does not support reflective programming.

Protected Functions

void indent(ostream&)
void indent_more()
void indent_less()
void begin_child(ostream&)
void end_child(ostream&)
void last_child(ostream&)
virtual void print(ostream&)
virtual void xprint(ostream&, string)

Protected Static Attributes

static int indent_level = 0
static bool branches[10000]

Friends

friend ostream &operator<<(ostream&, ast_node*)

Allow an AST node to be sent to an outstream for printing.

class ast_not : public ast_expression
#include <ast.hh>

A logical negation node.

Public Functions

ast_not(position_information*, ast_expression*)
virtual sym_index type_check()

Perform type checking. See semantic.cc for the method bodies. Note that it’s an error to call type_check in this class. It should only be called in the concrete AST nodes, see below.

virtual void optimize()

Perform optimization. See optimize.cc for the method bodies. Just as with type checking, it’s an error to call optimize in this class. It should only be called in the concrete AST nodes.

virtual sym_index generate_quads(quad_list&)

Generate quads. See quads.cc for the method bodies. Like type checking, generate_quads should only be called in concrete AST nodes.

Public Members

ast_expression *expr

The expression being negated.

Protected Functions

virtual void print(ostream&)
class ast_notequal : public ast_binaryrelation
#include <ast.hh>

Not-equal operator. a <> b.

Public Functions

ast_notequal(position_information*, ast_expression*, ast_expression*)
virtual sym_index type_check()

Perform type checking. See semantic.cc for the method bodies. Note that it’s an error to call type_check in this class. It should only be called in the concrete AST nodes, see below.

virtual void optimize()

Perform optimization. See optimize.cc for the method bodies. Just as with type checking, it’s an error to call optimize in this class. It should only be called in the concrete AST nodes.

virtual sym_index generate_quads(quad_list&)

Generate quads. See quads.cc for the method bodies. Like type checking, generate_quads should only be called in concrete AST nodes.

Protected Functions

virtual void print(ostream&)
class ast_optimizer
#include <optimize.hh>

Public Functions

void do_optimize(ast_stmt_list *body)

Starts recursive (destructive) optimization of a block.

This method has already been written for you, and is rather trivial. It is the interface to parser.y.

Parameters

body – a list of statements representing the block body to be optimized.

bool is_binop(ast_expression*)

Returns true if the argument is a subclass of ast_binaryoperation. It is needed to find out which nodes are eligible for optimization.

ast_expression *fold_constants(ast_expression*)

This is a convenient method used in optimize.cc. It has to be public so the ast_* nodes can access it. Another solution would be to make it a static method in the optimize.cc file… A matter of preference.

class ast_or : public ast_binaryoperation
#include <ast.hh>

Logical OR node. a OR b.

Public Functions

ast_or(position_information*, ast_expression*, ast_expression*)
virtual sym_index type_check()

Perform type checking. See semantic.cc for the method bodies. Note that it’s an error to call type_check in this class. It should only be called in the concrete AST nodes, see below.

virtual void optimize()

Perform optimization. See optimize.cc for the method bodies. Just as with type checking, it’s an error to call optimize in this class. It should only be called in the concrete AST nodes.

virtual sym_index generate_quads(quad_list&)

Generate quads. See quads.cc for the method bodies. Like type checking, generate_quads should only be called in concrete AST nodes.

inline virtual ast_or *get_ast_binaryoperation()

Protected Functions

virtual void print(ostream&)
class ast_procedurecall : public ast_statement
#include <ast.hh>

Represents a procedure call.

Example:

foo(1, 2+3, c);
AST for foo(1, 2+3, c) being called

Example:

fie(1);
AST for fie(1) being called

Example:

if a > b then
  c := a;
else
  c := b;
end;
AST for fum() being called

Public Functions

ast_procedurecall(position_information*, ast_id*, ast_expr_list*)
virtual sym_index type_check()

Perform type checking. See semantic.cc for the method bodies. Note that it’s an error to call type_check in this class. It should only be called in the concrete AST nodes, see below.

virtual void optimize()

Perform optimization. See optimize.cc for the method bodies. Just as with type checking, it’s an error to call optimize in this class. It should only be called in the concrete AST nodes.

virtual sym_index generate_quads(quad_list&)

Generate quads. See quads.cc for the method bodies. Like type checking, generate_quads should only be called in concrete AST nodes.

Public Members

ast_id *id

The procedure’s id node, contains a link to the symbol table.

ast_expr_list *parameter_list

A list of eventual parameters. If no parameters, this list is NULL.

Protected Functions

virtual void print(ostream&)
class ast_procedurehead : public ast_node
#include <ast.hh>

A node used to transfer information about an environment. Used in parser.y for setting the proper return type of a procedure. It is never part of a procedure body.

Public Functions

ast_procedurehead(position_information*, sym_index)
virtual void optimize()

Perform optimization. See optimize.cc for the method bodies. Just as with type checking, it’s an error to call optimize in this class. It should only be called in the concrete AST nodes.

virtual sym_index generate_quads(quad_list&)

Generate quads. See quads.cc for the method bodies. Like type checking, generate_quads should only be called in concrete AST nodes.

quad_list *do_quads(ast_stmt_list *s)

Starts the generation of a quad list for a program block pointed to by the argument. It then adds on the last label quad, and returns the list when it is done.

Parameters

s – the procedure body to generate quads for.

Public Members

sym_index sym_p

Pointer to the procedure in the symbol table.

Protected Functions

virtual void print(ostream&)
class ast_real : public ast_expression
#include <ast.hh>

A real node. Represents a real number, like 2.5.

Public Functions

ast_real(position_information*, double)
virtual sym_index type_check()

Perform type checking. See semantic.cc for the method bodies. Note that it’s an error to call type_check in this class. It should only be called in the concrete AST nodes, see below.

virtual void optimize()

Perform optimization. See optimize.cc for the method bodies. Just as with type checking, it’s an error to call optimize in this class. It should only be called in the concrete AST nodes.

virtual sym_index generate_quads(quad_list&)

Generate quads. See quads.cc for the method bodies. Like type checking, generate_quads should only be called in concrete AST nodes.

inline virtual ast_real *get_ast_real()

Public Members

double value

The floating point value of the node.

Protected Functions

virtual void print(ostream&)
class ast_return : public ast_statement
#include <ast.hh>

A return statement, used both for procedures and functions. If no value is returned (i.e. in a procedure), ‘value’ should be set to NULL.

Note that the return statement itself has no value, i.e., it cannot stand on the right-hand side of an assignment since it inherits ast_statement.

Public Functions

ast_return(position_information*)

Constructor for no return value.

ast_return(position_information*, ast_expression*)

Constructor with a return value.

virtual sym_index type_check()

Perform type checking. See semantic.cc for the method bodies. Note that it’s an error to call type_check in this class. It should only be called in the concrete AST nodes, see below.

virtual void optimize()

Perform optimization. See optimize.cc for the method bodies. Just as with type checking, it’s an error to call optimize in this class. It should only be called in the concrete AST nodes.

virtual sym_index generate_quads(quad_list&)

Generate quads. See quads.cc for the method bodies. Like type checking, generate_quads should only be called in concrete AST nodes.

Public Members

ast_expression *value

The return value.

Protected Functions

virtual void print(ostream&)
class ast_statement : public ast_node
#include <ast.hh>

The superclass for all DIESEL constructs that do not return a value.

Subclassed by ast_assign, ast_if, ast_procedurecall, ast_return, ast_while

Public Functions

ast_statement(position_information*)
virtual sym_index type_check()

Perform type checking. See semantic.cc for the method bodies. Note that it’s an error to call type_check in this class. It should only be called in the concrete AST nodes, see below.

virtual void optimize()

Perform optimization. See optimize.cc for the method bodies. Just as with type checking, it’s an error to call optimize in this class. It should only be called in the concrete AST nodes.

virtual sym_index generate_quads(quad_list&) = 0

Generate quads. See quads.cc for the method bodies. Like type checking, generate_quads should only be called in concrete AST nodes.

Protected Functions

virtual void print(ostream&)
class ast_stmt_list : public ast_node
#include <ast.hh>

Contains a list of statements. The body of a program, for example, is represented by this class: Example:

begin
  a := 1;
  b := a;
  c := b;
  d := c;
  e := d;
  f := e;
end;
digraph lab3_ast_stmt_list {
s1 [label="ast_stmt_list"];
s2 [label="ast_stmt_list"];
s3 [label="ast_stmt_list"];
s4 [label="ast_stmt_list"];
s5 [label="ast_stmt_list"];
s6 [label="ast_stmt_list"];
a1 [label="ast_assign"];
a2 [label="ast_assign"];
a3 [label="ast_assign"];
a4 [label="ast_assign"];
a5 [label="ast_assign"];
a6 [label="ast_assign"];
s1 -> s2;
s2 -> s3;
s3 -> s4;
s4 -> s5;
s5 -> s6;
s6 -> a1;
s5 -> a2;
s4 -> a3;
s3 -> a4;
s2 -> a5;
s1 -> a6;
a1 -> id1;
a1 -> 1;
a2 -> id3;
a2 -> id2;
a3 -> id5;
a3 -> id4;
a4 -> id7;
a4 -> id6;
a5 -> id9;
a5 -> id8;
a6 -> id11;
a6 -> id10;
id1 [label="a"];
id2 [label="a"];
id3 [label="b"];
id4 [label="b"];
id5 [label="c"];
id6 [label="c"];
id7 [label="d"];
id8 [label="d"];
id9 [label="e"];
id10 [label="e"];
id11 [label="f"];
}

Public Functions

ast_stmt_list(position_information*, ast_statement*)

Constructor for the first element of a list.

ast_stmt_list(position_information*, ast_statement*, ast_stmt_list*)

Constructor to add a new statement to the list.

virtual sym_index type_check()

Perform type checking. See semantic.cc for the method bodies. Note that it’s an error to call type_check in this class. It should only be called in the concrete AST nodes, see below.

virtual void optimize()

Perform optimization. See optimize.cc for the method bodies. Just as with type checking, it’s an error to call optimize in this class. It should only be called in the concrete AST nodes.

virtual sym_index generate_quads(quad_list&)

Generate quads. See quads.cc for the method bodies. Like type checking, generate_quads should only be called in concrete AST nodes.

Public Members

ast_statement *last_stmt

Points to the last (last added) statement in the list.

ast_stmt_list *preceding

Points to a list of the preceding statements. Is NULL when empty.

Protected Functions

virtual void print(ostream&)
class ast_sub : public ast_binaryoperation
#include <ast.hh>

Minus node. a - b.

Public Functions

ast_sub(position_information*, ast_expression*, ast_expression*)
virtual sym_index type_check()

Perform type checking. See semantic.cc for the method bodies. Note that it’s an error to call type_check in this class. It should only be called in the concrete AST nodes, see below.

virtual void optimize()

Perform optimization. See optimize.cc for the method bodies. Just as with type checking, it’s an error to call optimize in this class. It should only be called in the concrete AST nodes.

virtual sym_index generate_quads(quad_list&)

Generate quads. See quads.cc for the method bodies. Like type checking, generate_quads should only be called in concrete AST nodes.

inline virtual ast_sub *get_ast_binaryoperation()

Protected Functions

virtual void print(ostream&)
class ast_uminus : public ast_expression
#include <ast.hh>

A unary minus node.

Note that there is no unary plus.

Public Functions

ast_uminus(position_information*, ast_expression*)
virtual sym_index type_check()

Perform type checking. See semantic.cc for the method bodies. Note that it’s an error to call type_check in this class. It should only be called in the concrete AST nodes, see below.

virtual void optimize()

Perform optimization. See optimize.cc for the method bodies. Just as with type checking, it’s an error to call optimize in this class. It should only be called in the concrete AST nodes.

virtual sym_index generate_quads(quad_list&)

Generate quads. See quads.cc for the method bodies. Like type checking, generate_quads should only be called in concrete AST nodes.

Public Members

ast_expression *expr

The expression to negate.

Protected Functions

virtual void print(ostream&)
class ast_while : public ast_statement
#include <ast.hh>

A while loop. Contains a test condition and a loop body.

Compare this to other conditional nodes (e.g. ast_elsif, ast_if).

Public Functions

ast_while(position_information*, ast_expression*, ast_stmt_list*)
virtual sym_index type_check()

Perform type checking. See semantic.cc for the method bodies. Note that it’s an error to call type_check in this class. It should only be called in the concrete AST nodes, see below.

virtual void optimize()

Perform optimization. See optimize.cc for the method bodies. Just as with type checking, it’s an error to call optimize in this class. It should only be called in the concrete AST nodes.

virtual sym_index generate_quads(quad_list&)

Generate quads. See quads.cc for the method bodies. Like type checking, generate_quads should only be called in concrete AST nodes.

Public Members

ast_expression *condition

The test condition.

ast_stmt_list *body

The loop body.

Protected Functions

virtual void print(ostream&)
class code_generator
#include <codegen.hh>

Public Functions

code_generator(const string)
~code_generator()
void generate_assembler(quad_list*, symbol *env)

This interface method is called from parser.y to start assembler expansion of a code block represented as a quad list.

Private Functions

int align(int)

Aligns a stack frame on an 8-byte boundary.

void prologue(symbol*)

Generates code to create the activation record.

This includes the display area and allocating space for local variables and temporaries.

void epilogue(symbol*)

Generates code to release the activation record.

void expand(quad_list *q)

Translates a quad list to assembler code using the methods above. You will need to write code for expanding q_param and q_call quads.

void find(sym_index, int*, int*)

Returns the display register level and offset for a variable, array or parameter to the symbol table.

Note that parameters are stored in the caller’s activation record and therefore have positive offset while local and temporary variables have negative offset.

Also note that the method modifies its arguments, which is why they are passed as pointers.

void fetch(sym_index, const register_type)

Retrieves the value of a variable, parameter or constant to a given register.

void fetch_float(sym_index)

Pushes the value of a variable, paramters or constant to the FPU.

Note that this method will never generate code for constant integers but will for constant reals.

void store(const register_type, sym_index)

Stores the value of a register in a variable or parameter.

void store_float(sym_index)

Pops the FPU stack and stores the value in a variable or parameter.

void array_address(sym_index, const register_type)

Retrieves the base address of an array to a register.

The method is called when expanding the quadruples q_lindex, q_irindex, and q_rrindex.

void frame_address(int level, const register_type)

Given a lexical level and a register, stores the base address of the corresponding frame from the display area.

Private Members

string reg[3]
ofstream out
class constant_symbol : public symbol
#include <symtab.hh>

Derived symbol type, used for constants.

Public Functions

constant_symbol(const pool_index)
inline virtual constant_symbol *get_constant_symbol()

Public Members

constant_value const_value

Value of a constant, can be int or float. The value is stored in a union; check the #type attribute to figure out if you should access ivar or rvar.

Protected Functions

virtual void print(ostream&)
union constant_value
#include <symtab.hh>

Public Members

long ival
double rval
class function_symbol : public symbol
#include <symtab.hh>

Derived symbol type, used for functions.

Public Functions

function_symbol(const pool_index)
inline virtual function_symbol *get_function_symbol()

Public Members

int ar_size

Activation record size.

Specifies the memory space that is needed to store the local variables belonging to the procedure/function on the runtime stack. A procedure that declares an array of ten integers, two reals and three integers requires (10+2+3)*8 bytes.

int label_nr

The label number which is assigned to the procedure or function in assembler code.

parameter_symbol *last_parameter

List of parameters. We store them in reverse order to make type-checking easier later on.

Protected Functions

virtual void print(ostream&)
class nametype_symbol : public symbol
#include <symtab.hh>

Derived symbol type, used for nametypes. Since it contains no new data fields, we might as well use symbol directly. This subclass mainly exists for abstraction’s sake.

Public Functions

nametype_symbol(const pool_index)
inline virtual nametype_symbol *get_nametype_symbol()

Protected Functions

virtual void print(ostream&)
class parameter_symbol : public symbol
#include <symtab.hh>

Parameters require particular checking, as the formal parameters are to be bound to the actual ones. Assume the following declaration:

procedure p(i : integer; r : real);

which is then called with the following actual parameters:

p(22/7, 355/113);

To perform a semantic check, we must find the formal parameters (i and r) in the symbol table and compare their type attributes with the actual parameters. In this example a type conflict occurs when the value of the expression 22/7 (which is a real) is to be bound to the integer variable i.

For this reason the formal parameters are linked together in the symbol table. The preceding attribute points to the previous parameter in the link chain. The procedure (or function) points to the last parameter. It might seem counter-intuitive that the parameters are stored backwards like this, but it will actually make life easier later on.

The order of storing the actual paramerers.

The handling of parameters is actually one of the trickiest parts to understand correctly in this entire lab course. Some work has already been done for you, but you will have to do a lot of it yourself. Make sure to read the code comments carefully when dealing with parameters, and be sure to have this lab material handy as well. The order of the parameters will be flipped around during compiling more than once. Once you have that sequence clear, getting them to work will be a lot easier.

Public Functions

parameter_symbol(const pool_index)
inline virtual parameter_symbol *get_parameter_symbol()

Public Members

int size

Nr of bytes parameter needs.

parameter_symbol *preceding

Link to preceding parameter, if any.

Protected Functions

virtual void print(ostream&)
class position_information
#include <error.hh>

Public Functions

position_information()
position_information(int l, int c)
int get_line()
int get_column()

Private Members

int line
int column
class procedure_symbol : public symbol
#include <symtab.hh>

Derived symbol type, used for procedures.

Public Functions

procedure_symbol(const pool_index)
inline virtual procedure_symbol *get_procedure_symbol()

Public Members

int ar_size

Activation record size.

Specifies the memory space that is needed to store the local variables belonging to the procedure/function on the runtime stack. A procedure that declares an array of ten integers, two reals and three integers requires (10+2+3)*8 bytes.

int label_nr

The label number which is assigned to the procedure or function in assembler code.

parameter_symbol *last_parameter

List of parameters. We store them in reverse order to make type-checking easier later on.

Protected Functions

virtual void print(ostream&)
class quad_list
#include <quads.hh>

Public Functions

quad_list(int)
quad_list &operator+=(quadruple *q)

Public Members

int last_label

Private Functions

void print(ostream&)

Private Members

quad_list_element *head
quad_list_element *tail
int quad_nr

Friends

friend class quad_list_iterator
friend ostream &operator<<(ostream&, quad_list*)
class quad_list_element
#include <quads.hh>

Public Functions

quad_list_element(quadruple*, quad_list_element*)

Public Members

quadruple *data
quad_list_element *next
class quad_list_iterator
#include <quads.hh>

Public Functions

quad_list_iterator(quad_list *q_list)
quadruple *get_current()
quadruple *get_next()

Private Members

quad_list_element *current
class quadruple
#include <quads.hh>

Public Functions

quadruple(quad_op_type, sym_index, sym_index, sym_index)

Public Members

quad_op_type op_code
sym_index sym1
sym_index sym2
sym_index sym3
long int1
long int2
long int3

Private Functions

void print(ostream&)

Friends

friend ostream &operator<<(ostream&, quadruple*)
class semantic
#include <semantic.hh>

Public Functions

void check_parameters(ast_id*, ast_expr_list*)

Compare formal vs. actual parameters in function/procedure calls.

It is also responsible for making sure that parameters are type-checked. You will have to write this method yourself, and also figure out from where to call it.

void do_typecheck(symbol *env, ast_stmt_list *body)

Initiate type checking of a block of code.

Performs some initialisation before starting recursive type checking of a block.

The function has already been written for you.

Parameters
  • env – the symbol representing the block to be checked (i.e., a function or procedure symbol)

  • body – an ast_stmt_list node representing the block body to be checked.

sym_index check_binop1(ast_binaryoperation*)
sym_index check_binop2(ast_binaryoperation*, string)
sym_index check_binrel(ast_binaryrelation*)

Private Functions

bool chk_param(ast_id*, parameter_symbol*, ast_expr_list*)
class symbol
#include <symtab.hh>

The symbol table consists of entries of subclasses to symbol. This class contains data that are common to all symbol types. This class is never used directly. Use the derived classes instead.

Subclassed by array_symbol, constant_symbol, function_symbol, nametype_symbol, parameter_symbol, procedure_symbol, variable_symbol

Public Functions

symbol(pool_index)
inline virtual constant_symbol *get_constant_symbol()
inline virtual variable_symbol *get_variable_symbol()
inline virtual array_symbol *get_array_symbol()
inline virtual parameter_symbol *get_parameter_symbol()
inline virtual procedure_symbol *get_procedure_symbol()
inline virtual function_symbol *get_function_symbol()
inline virtual nametype_symbol *get_nametype_symbol()

Public Members

pool_index id

Index to the string_pool, ie, its name.

The object’s identifier is stored in the string table (i.e. the spelling, see previous lab). Index to string table (called id in the lab skeleton) is a value denoting where an identifier starts in the string table. Example:

The scanner from lab1 has created a string table that look like this (the number denotes the length of the following identifier):

7GLOBAL.4VOID7INTEGER...

The id to GLOBAL. is 0 because it starts on the first position in the string table (the index to the first position in an array in C++ is 0).

sym_type tag

This field is only used to give us a way to differentiate between the various symbol classes when we need to do a safe downcast from the superclass. The default value of tag is SYM_UNDEF.

sym_index type

In practice type is used only by constants, variables, arrays, parameters and functions; all other symbol types are of type void. The type field contains an index to a name type which in DIESEL only can be integer_type, real_type, or void_type. These will be preinstalled in the symbol table.

sym_index hash_link

If the object you are searching for not is found immediately after the key transformation, you follow the hash link backwards in the table.

hash_index back_link

This link points back to the hash table from the symbol table and can be used to speed up certain types of lookups. It is possible to complete the lab without using it, though.

block_level level

The lexical level states how deeply nested the object is in the program. For example, an object on the first level is global. Eight levels is the maximum in Diesel.

int offset

Offset specifies which relative position the object has in the memory space which has been reserved for a given lexical level on the runtime stack. This is used during code generation.

Protected Types

enum format_types

Values:

enumerator LONG_FORMAT
enumerator SUMMARY_FORMAT
enumerator SHORT_FORMAT
typedef enum format_types format_type

Protected Functions

virtual void print(ostream&)

Protected Static Attributes

static format_type output_format = symbol::LONG_FORMAT

Friends

friend ostream &long_symbols(ostream&)
friend ostream &summary_symbols(ostream&)
friend ostream &short_symbols(ostream&)
friend ostream &operator<<(ostream&, symbol*)
class symbol_table
#include <symtab.hh>

Public Functions

symbol_table()
long ieee(double)
int get_size(const sym_index)
pool_index pool_install(char*)

Install a string (an identifier or a string constant) in the string pool. Returns the index to the installed string.

char *pool_lookup(const pool_index)

Given a pool_index into the string pool, returns the string it points to.

bool pool_compare(const pool_index, const pool_index)

Compare two strings taking their respective pool_index as arguments. Returns true if they are identical, and false otherwise. Note that it is not enough to just check that the indices are identical. Why not?

pool_index pool_forget(const pool_index)

Remove last installed entry from the string pool. This will only be useful if you opt to implement shared strings.

char *fix_string(const char*)

Remove double '' in strings constants.

Internalizes a string constant. You hopefully use this method in several labs.

char *capitalize(const char*)

Capitalizes a string. You hopefully use this method in several labs.

hash_index hash(const pool_index)

Given a pool_index to a string, return its hash index.

symbol *get_symbol(const sym_index)
sym_index install_symbol(const pool_index, const sym_type tag)

Installs a symbol in the symbol table and returns its index. This method installs a symbol in the symbol table and returns its index. If the symbol already existed at the same lexical level (i.e. two objects with the same identifi and value of the attribute level), the object will not be installed, and the index of the symbol that already exists is returned.

If a symbol is installed, its tag will be set to SYM_UNDEF (which is done by default in the symbol constructor). The appropriate tag will then be set in the enter_ method that called symbol_table::install_symbol. In this way name collisions can be discovered. For instance: The variable m is declared twice on the same lexical level, and that is of course not correct. When the first m is encountered the method symbol_table::enter_variable will try to install the symbol in the symbol table. It calls symbol_table::install_symbol which creates and install a symbol of the correct type. The symbol’s tag is now set to SYM_UNDEF. When symbol_table::install_symbol finishes, the execution continues inside symbol_table::enter_variable and the method checks if the recent installed symbol’s tag is set to SYM_UNDEF, and if it’s true, it changes the tag to SYM_VAR. When the second m is encountered the procedure is repeated, but this time symbol_table::install_symbol returns the sym_index to the first m. When checking the tag of this symbol, symbol_table::enter_variable gets the answer SYM_VAR and not SYM_UNDEF. Therefore symbol_table::enter_variable knows that a symbol with this name already exists in the symbol table at this lexical level and outputs an error message. The second m is never installed. Furthermore, the level attribute will be set to the current lexical level, symbol_table#current_level (which is currently zero).

Note that the first argument to symbol_table::install_symbol is an index to the string table (symbol_table#string_pool). This is, of course, what the scanner outputs in the YYSTYPE::pool_p field of the yylval union. If you had problems understanding what it was for earlier, hopefully some of that has become more clear now!

The second argument is the tag that the symbol should have, it denotes which symbol class should be used (constant_symbol, variable_symbol, etc.).

sym_index lookup_symbol(const pool_index)

Look for a symbol with the same identifier as the argument specifies in the string table, and returns its symbol table index. The search is performed according to the scoping rules, i.e. from the current lexical level and outwards. Make sure you understand what this means! If the object is not found, NULL_SYM is returned.

void print(int)

Very useful method for dumping the symbol table to stdout. The value of the argument determines what info will be printed:

1

Print one line of info per symbol table entry.

2

Print (only) the string table, showing the current pool_pos.

3

Print (only) the non-zero elements in the hash table.

any other

Print detailed information about every symbol in the symbol table. Watch out, though: this gets very long if you have more than a few symbols installed.

pool_index get_symbol_id(const sym_index)

Given a symbol table index, return its pool_index, or 0 (zero) if no such symbol existed.

sym_index get_symbol_type(const sym_index)

Given a symbol table index, returns its type (void_type, integer_type, or real_type). If the symbol didn’t exist, returns void_type.

sym_type get_symbol_tag(const sym_index)

Given a symbol table index, returns its tag type (i.e., the tag marking which symbol class it is: SYM_CONST, SYM_VAR).

void set_symbol_type(const sym_index, const sym_index)

Given a symbol table index to a symbol, and a symbol table index to a type (e.g. integer_type etc.), sets the symbol to that type. You shouldn’t need to use it in this lab.

long get_next_label()
sym_index gen_temp_var(sym_index)

Given a symbol table index to a type (e.g., integer_type etc.), generates, installs and returns index to a temporary variable of that type. It is not meaningful to generate a temporary variable of void_type.

Choose a method for giving the temporary variables names which can not collide with the user.

Note

The implementation used for the traces returns identifiers with exactly length MAX_TEMP_VAR_LENGTH. It pads the remainder of the identifier with spaces, giving temporary names such as:

"$1      "
"$2      "
// ...
"$1234   "
sym_index enter_constant(position_information*, const pool_index, const sym_index, const long)

Given position information, a pool_index to an identifier’s name, a sym_index to the desired type and an int representing the actual constant value, generate and install a constant of the desired type.

sym_index enter_constant(position_information*, const pool_index, const sym_index, const double)

Given position information, a pool_index to an identifier’s name, a sym_index to the desired type and an int representing the actual constant value, generate and install a constant of the desired type.

sym_index enter_variable(position_information*, const pool_index, const sym_index)

Given position information, a pool_index to an identifier’s name, and a sym_index to the desired type, generate and install a variable of the desired type.

sym_index enter_variable(const pool_index, const sym_index)

This convenience method is used for installing temporary variables for which position information is not relevant. See quads.cc (lab6).

sym_index enter_array(position_information*, const pool_index, const sym_index, const int)

Given position information, a pool_index to an identifier’s name, a sym_index to the desired type, and an array cardinality, generate and install an array of the desired type.

sym_index enter_function(position_information*, const pool_index)

Given position information, and a pool_index to an identifier’s name, generate and install a function.

The type is set later on since it will not be known at symbol install time.

sym_index enter_procedure(position_information*, const pool_index)

Given position information, and a pool_index to an identifier’s name, generate and install a procedure.

sym_index enter_parameter(position_information*, const pool_index, const sym_index)

Given position information, a pool_index to an identifier’s name, and a sym_index to the desired type, generate and install a parameter of the desired type.

sym_index enter_nametype(position_information*, const pool_index)

Given position information, and a pool_index to an identifier’s name, generate and install a nametype of the desired type.

Since DIESEL doesn’t allow user-defined types, you will never need to use this method. It is used from the symbol table class constructor to install the default types, though.

NOTE: Should perhaps be marked private?

sym_index current_environment()

Returns the symbol table index to the current environment, i.e., procedure/function.

void open_scope()

Opens a new lexical level.

The routine is called when the name of a procedure or function has been installed. Note that proc lies outside the lexical level that i and loc have, in this example:

procedure proc(i : integer);
  var loc : integer;
begin
end;

The #block_tablekeeps track of the active blocks in the program. When we have treated the whole proc procedure above, we must make sure that the loc variable is not “visible”. We shall soon see how this is achieved.

sym_index close_scope()

Closes the current lexical level and returns a symbol table index to the new lexical level.

The routine is called when we have finished with a procedure or function. The local variables that were there will then become “invisible”, i.e., the following program code can not reference them.

Private Members

char *string_pool

The actual string pool.

long pool_length
long pool_pos
sym_index *hash_table
block_level current_level

Current nesting depth.

sym_index *block_table

Table of level sym_index pointers. They point at the start of a new scope/block.

symbol **sym_table
sym_index sym_pos
int label_nr
long temp_nr
class variable_symbol : public symbol
#include <symtab.hh>

Derived symbol type, used for variables.

Public Functions

variable_symbol(const pool_index)
inline virtual variable_symbol *get_variable_symbol()

Protected Functions

virtual void print(ostream&)
struct YYLTYPE
#include <scanner.hh>

For every token, these fields should be set to the correct position in the source code file.

Public Members

int first_line
int first_column
int last_line
int last_column
union YYSTYPE
#include <scanner.hh>

Public Members

long ival

Value of an integer T_INTNUM

double rval

Value of a real T_REALNUM

pool_index str

Value of a string T_STRINGCONST

pool_index pool_p

Value of an identifier T_IDENT

namespace std
file ast.cc
#include “ast.hh

Functions

ostream &operator<<(ostream &o, ast_node *node)
file ast.hh
#include “symtab.hh
#include “quads.hh

Typedefs

typedef enum ast_node_types ast_node_type

Enums

enum ast_node_types

Values:

enumerator AST_NODE
enumerator AST_STATEMENT
enumerator AST_EXPRESSION
enumerator AST_BINARYOPERATION
enumerator AST_BINARYRELATION
enumerator AST_LVALUE
enumerator AST_EXPR_LIST
enumerator AST_STMT_LIST
enumerator AST_ELSIF_LIST
enumerator AST_ID
enumerator AST_INDEXED
enumerator AST_ADD
enumerator AST_SUB
enumerator AST_OR
enumerator AST_AND
enumerator AST_MULT
enumerator AST_DIVIDE
enumerator AST_IDIV
enumerator AST_MOD
enumerator AST_EQUAL
enumerator AST_NOTEQUAL
enumerator AST_LESSTHAN
enumerator AST_GREATERTHAN
enumerator AST_PROCEDURECALL
enumerator AST_ASSIGN
enumerator AST_WHILE
enumerator AST_IF
enumerator AST_RETURN
enumerator AST_FUNCTIONCALL
enumerator AST_UMINUS
enumerator AST_NOT
enumerator AST_ELSIF
enumerator AST_INTEGER
enumerator AST_REAL
enumerator AST_FUNCTIONHEAD
enumerator AST_PROCEDUREHEAD
enumerator AST_PARAMETER
enumerator AST_CAST

Functions

ostream &operator<<(ostream&, ast_node*)
file codegen.cc
#include <iostream>
#include <iomanip>
#include <fstream>
#include <stdio.h>
#include <string.h>
#include “symtab.hh
#include “quads.hh
#include “codegen.hh

Variables

bool assembler_trace
code_generator *code_gen = new code_generator("d.out")
file codegen.hh
#include <fstream>
#include “quads.hh
#include “symtab.hh

Enums

enum register_type

Values:

enumerator RAX
enumerator RCX
enumerator RDX

Variables

const int MAX_PARAMETERS = 127
const int STACK_WIDTH = 8
file diesel_rts.c
#include <stdio.h>

Functions

void myputchar(int ch)
file error.cc
#include <cstdlib>
#include “error.hh

Functions

ostream &error(string header)
ostream &error(position_information *pos)
void fatal(string msg)

Prints message, aborts compiling.

void yyerror(string msg)

This must be defined (for flex), but using error(pos) << "foo" is preferable.

ostream &type_error()
ostream &type_error(position_information *pos)
ostream &debug(string header)
ostream &debug(position_information *pos)

Variables

int error_count = 0
file error.hh
#include <iostream>
#include <sstream>
#include <ostream>

Functions

void fatal(string)

Prints message, aborts compiling.

void yyerror(string)

This must be defined (for flex), but using error(pos) << "foo" is preferable.

ostream &error(string header = "Error: ")
ostream &error(position_information*)
ostream &type_error()
ostream &type_error(position_information*)
ostream &debug(string header = "Debug: ")
ostream &debug(position_information*)

Variables

int error_count
int yylineno
file main.cc
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include “ast.hh
#include “parser.hh”

Functions

void usage(char *program_name)
int main(int argc, char **argv)

Variables

int error_count
bool yydebug
bool assembler_trace = false
bool print_ast = false
bool print_quads = false
bool typecheck = true
bool optimize = true
bool quads = true
bool assembler = true
file optimize.cc
#include “optimize.hh

Variables

ast_optimizer *optimizer = new ast_optimizer()
file optimize.hh
#include “ast.hh

Variables

ast_optimizer *optimizer
file quads.cc
#include <iostream>
#include <iomanip>
#include <stdio.h>
#include “symtab.hh
#include “ast.hh
#include “quads.hh

Defines

USE_Q

Functions

ostream &operator<<(ostream &o, quadruple *q)
ostream &operator<<(ostream &o, quad_list *q_list)
file quads.hh
#include “ast.hh

Enums

enum quad_op_type

Values:

enumerator q_rload
enumerator q_iload
enumerator q_inot
enumerator q_ruminus
enumerator q_iuminus
enumerator q_rplus
enumerator q_iplus
enumerator q_rminus
enumerator q_iminus
enumerator q_ior
enumerator q_iand
enumerator q_rmult
enumerator q_imult
enumerator q_rdivide
enumerator q_idivide
enumerator q_imod
enumerator q_req
enumerator q_ieq
enumerator q_rne
enumerator q_ine
enumerator q_rlt
enumerator q_ilt
enumerator q_rgt
enumerator q_igt
enumerator q_rstore
enumerator q_istore
enumerator q_rassign
enumerator q_iassign
enumerator q_call
enumerator q_rreturn
enumerator q_ireturn
enumerator q_lindex
enumerator q_rrindex
enumerator q_irindex
enumerator q_itor
enumerator q_jmp
enumerator q_jmpf
enumerator q_param
enumerator q_labl
enumerator q_nop
file scanner.hh
#include “symtab.hh

Defines

T_EOF
T_ERROR
T_DOT
T_SEMICOLON
T_EQ
T_COLON
T_LEFTBRACKET
T_RIGHTBRACKET
T_LEFTPAR
T_RIGHTPAR
T_COMMA
T_LESSTHAN
T_GREATERTHAN
T_ADD
T_SUB
T_MUL
T_RDIV
T_OF
T_IF
T_DO
T_ASSIGN
T_NOTEQ
T_OR
T_VAR
T_END
T_AND
T_IDIV
T_MOD
T_NOT
T_THEN
T_ELSE
T_IDENT
T_CONST
T_ARRAY
T_BEGIN
T_WHILE
T_ELSIF
T_INTNUM
T_RETURN
T_PROGRAM
T_REALNUM
T_FUNCTION
T_PROCEDURE
T_STRINGCONST
NR_SYMS

Variables

YYSTYPE yylval

Contains the additional attributes needed to describe certain tokens (integers, reals, string constants, and identifiers).

YYLTYPE yylloc

Contains variables for tracking position in the source code.

file semantic.cc
#include “semantic.hh

Variables

semantic *type_checker = new semantic()
static bool has_return = false
file semantic.hh
#include “ast.hh

Variables

semantic *type_checker
file symbol.cc
#include “symtab.hh

Functions

ostream &operator<<(ostream &o, symbol *sym)
ostream &short_symbols(ostream &o)
ostream &summary_symbols(ostream &o)
ostream &long_symbols(ostream &o)
file symtab.cc
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#include <iomanip>
#include <fstream>
#include <iostream>
#include <ctype.h>
#include <string.h>
#include “symtab.hh

Variables

symbol_table *sym_tab = new symbol_table()

A global pointer to the symbol_table.

You will use functions in the symbol_table to install strings (and, if you want to make your compiler use shared strings, to reduce memory requirements, to look-up and identify symbols, as well as forget strings).

sym_index void_type
sym_index integer_type
sym_index real_type
file symtab.hh
#include “error.hh

Defines

TEST_SCANNER

Typedefs

typedef long pool_index
typedef long hash_index
typedef long sym_index
typedef int block_level
typedef enum symbol_types sym_type

Enums

enum symbol_types

Values:

enumerator SYM_ARRAY
enumerator SYM_FUNC
enumerator SYM_PROC
enumerator SYM_VAR
enumerator SYM_PARAM
enumerator SYM_CONST
enumerator SYM_NAMETYPE
enumerator SYM_UNDEF

Functions

ostream &short_symbols(ostream&)
ostream &summary_symbols(ostream&)
ostream &long_symbols(ostream&)
ostream &operator<<(ostream&, symbol*)

Variables

const block_level MAX_BLOCK = 8

Max allowed nesting levels (size of block table).

const hash_index MAX_HASH = 512

Max size of hash table.

const pool_index BASE_POOL_SIZE = 1024

Base size of string pool.

const sym_index MAX_SYM = 1024

Max size of symbol table.

const sym_index NULL_SYM = -1

Signifies ‘no symbol’.

const int ILLEGAL_ARRAY_CARD = -1

Signifies a non-int array size. The code using this constant has already been written for you.

const int MAX_TEMP_VARS = 999999

Sets a limit for max nr of temporary variables. Should never be reached unless someone really, really starts to dig writing huge programs in Diesel. See quads.cc.

const int MAX_TEMP_VAR_LENGTH = 8
symbol_table *sym_tab

A global pointer to the symbol_table.

You will use functions in the symbol_table to install strings (and, if you want to make your compiler use shared strings, to reduce memory requirements, to look-up and identify symbols, as well as forget strings).

sym_index void_type
sym_index integer_type
sym_index real_type
dir /mnt/c/home/adrpo33/dev/repos/teaching/tddb44/webpage/TDDB44-lab-instructions/code
dir /mnt/c/home/adrpo33/dev/repos/teaching/tddb44/webpage/TDDB44-lab-instructions/code/remaining