Expressions

Expression in Turbo Pascal is everything from constant, variable, calculation or just an identifier. Expressions are made up of operators and operands. Most Pascal operators are binary; they take two operands. The rest are unary and take only one operand. This unit contains over 100 functions to process every possible Turbo Pascal expression.

Expressions are one of the core elements of Turbo Pascal. Expression is everything from variables, calculations, addresses or functions. Most of the code generated by Turbo Pascal comes from expressions. Therefore, expression processing needs a lot of functions and procedures that deal with all possible cases.

These routines provide basic expression processing. Each expression is divided into simpler elements according to the operator precedence: Expression, Simple Expression, Term and Factor. Once the operation and operands are determined, the mathematical operation can be performed.

This procedure loads variable data from symbol table and sets all expression fields accordingly. Variables declared outside procedures and functions are called global variables, and they reside in the data segment. Variables declared within procedures and functions are called local variables, and they reside in the stack segment. Absolute variables are loaded from the target …

This procedure loads numeric constants and constant identifiers to the expression. For constant identifiers the constant value is first loaded to NumericConstant variable from where it is moved to the Value field of the expression.

This procedure processes unary plus or minus before a factor. Plus is skipped while minus changes the sign if expression is integer, extended or real.

This procedure performs logical operation NOT. For boolean values it negates the value, for constant integer expressions it negates the value and for other integer expressions it generates code for NOT instruction.

This procedure processes type identifiers in expression. If a period follows an object type then this is a method call. If left parenthesis follows then this is a typecast.

This procedure processes set constants. This can be an empty set, or a collection of set elements and ranges. If the elements are not constant the code is generated to include them into the set. Individual elements as well as range limits are treated as expressions.

Turbo Pascal Provides two ‘virtual’ arrays that provide acces to memory (Mem) and I/O ports (Port). To access memory we need segment and offset. Both parameters are processed and calculated. The segment is put into the ES register while offset is in DI.

Turbo Pascal expects different expression types depending on the context or language syntax. For this purposes there are many functions and procedures that process expression and report error if the expression doesn’t meet expectations.

This procedure converts expression between compatible types. For example, Integer values can be assigned to Real variables, only the expression and type need to be converted.

This procedure checks type compatibility between expression and specified type and sets expression to the specified type if needed.

This procedure extends integers to the new integer type.

Turbo Pascal uses few functions and procedures to process, check and convert strings – Char type and zero-based character arrays can also be considered as strings.

Turbo Pascal sometimes needs to convert number types. Integer and Real can be used as Extended and Integer or Extended can be used as Real.

Turbo Pascal checks if values are within limits. If the value is constant it is immediately checked for lower and upper limit. If it is not constant and range checking is enabled, Turbo Pascal will generate range checking code. Botl limits will be stored in code and compiler procedure will be called to check range …

Turbo Pascal uses a very interesting way of dealing with boolean expressions. Each boolean expression contains a conditional opcode to jump if the expression evaluates to True and two linked lists of jumps. One list contains jumps to code which executes when the expression evaluates to True while the other list contains jumps to code …

Loading an expression into registers is a very common task in Turbo Pascal compiler. These two functions take care for this. They generate instructions to load the expression regardless where it is located.

During expression calculation Turbo Pascal stores the value in registers. However, they are limited and some operations can be performed only with specific registers. Therefore, Turbo Pascal compiler needs some way to save and restore expressions.

This procedure creates a pointer to any variable reference including procedures and functions. It is used for the @ operator and system functions Addr, Seg and Ofs. Its reverse operation is pointer dereference.