Expression Elements

Expressions are one of 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. The TExpression object listed below uses few data structures that define the expression at any stage:
  • Type Definition Pointer
  • Intermediate Code Offset
  • Location
  • Data Type
  • Used Registers
  • Location Data
  • Value
Type Definition Pointer points to the type definition record. This pointer defines the type of the expression. Intermediate Code Offset is the offset in the intermediate code table. This is the intermediate code subroutine for this expression. because it is possible to call other intermediate code subroutines it is easily possible to create code step by step taking into account used registers. Location specifies the location of the expression. It can be memory, register or register set, constant, pointer to memory, boolean value, stack, stack frame or procedure.
    TExpressionLocation = (elMemory, elRegister, elConstant, elPointerToMemory, elBoolean, elStack, elStackFrame, elProcedure);
Data Type specifies how should the integer value be interpreted.
     TIntegerType = (itSigned, itUnsigned, it16Bit, it32Bit, it32Bytes);

     TIntegerTypeSet = Set of TIntegerType;
Used Registers mark which registers has this expression used or changed. With this information it is possible to generate effective code that uses registers in the most efficient way.
    TUsedRegister = (urAX, urCX, urDX, urBX, urSP, urBP, urSI, urDI); { [urDI, urSI, urBP, urSP, urBX, urDX, urCX, urAX] }

    TUsedRegistersSet = Set of TUsedRegister;
Location Data provides additional information about the expression.
    TExpressionLocationData = Record
                                Case TExpressionLocation of
                                  elMemory: (Flags: TExpressionFlagsSet);
                                  elRegister: (Register: Byte);
                                  elBoolean: (JumpIfTrueOpCode: Byte);
                                  elProcedure: (B9: Byte);
                              end;
    TExpressionFlag = (segDS, segSS, segCS, segES, ofsBP, ofsDI, efTypedConstant, efReadOnly);

    TExpressionFlagsSet = Set of TExpressionFlag;   { [efReadOnly, efTypedConstant, ofsDI, ofsBP, segES, segCS, segSS, segDS] }
Value provides actual expression value. It can be interpreted in many ways.
    PValue = ^TValue;
    TValue = Record
               Case Byte of
                 0: (Byte, Byte1: Byte);
                 1: (Word, W12, W14, W16, W18: Word);
                 2: (LongInt: LongInt);
                 3: (Pointer: Pointer);
                 4: (VarOffset, VarSegment: Word);
                 5: (LongRec: LongRec);
                 6: (As10Bytes: T10Bytes);
                 7: (Extended: Extended);
                 8: (Real: Real);
                 9: (PtrOffset, PtrSegment: Word);
                 10:(Integer: Integer);
                 11:(ShortInt: ShortInt);
                 12:(Real1, Real2, Real3: Word);
                 13:(Single: Single);
                 14:(Double: Double);
                 15:(Comp: Comp);
                 16:(Char: Char);
                 17:(Offset,  BlockRecord,  Segment:  Word; ObjectTypeDefinition: PObjectTypeDefinition);
                 18:(Offset_, BlockRecord_, Segment_: Word; IdentifierDataPointer: Pointer);
                 19:(LastJumpToTrue, LastJumpToFalse: Word);
                 20:(ShortCircuitJumps: LongInt);
                 21:(Words: Array [0..4] of Word);
             end;
The TExpression object has more than 100 methods that take care for all types of expressions.
    PExpression = ^TExpression;
    TExpression = Object
                    TypeDefPtr:             PTypeDefinition;
                    IntermediateCodeOffset: Word;
                    Location:               TExpressionLocation;
                    DataType:               TIntegerTypeSet;
                    UsedRegisters:          TUsedRegistersSet;
                    LocationData:           TExpressionLocationData;
                    Value:                  TValue;

                    Procedure ProcessExpression;
                    Procedure ProcessSimpleExpression;
                    Procedure ProcessTerm;
                    Procedure ProcessFactor;
                    Function  ProcessQualifiers: Boolean;
                    Procedure DereferencePointer;
                    Procedure ProcessProcedureIdentifier;
                    Procedure ProcessLeftParenthesis;
                    Procedure ProcessValueTypecastOrMethodCall;
                    Procedure Process_INHERITED;
                    Procedure Process_SystemFunction;
                    Procedure Process_At;
                    Procedure Process_Mem;
                    Procedure LoadVariable;
                    Procedure AdjustExpressionToType (TypeDef: PTypeDefinition);
                    Procedure CheckTypeCompatibility (SpecifiedType: PTypeDefinition);
                    Procedure ExtendInteger (NewIntegerType: TIntegerTypeSet);
                    Procedure CalculateExpression;
                    Procedure CalculateExpressionWithType (ExpectedType: PTypeDefinition);
                    Function  IsConstantString (Size: Word; Var StringLen: Byte): Boolean;
                    Procedure SetExpressionToStringTypedConstant (Str: Pointer; Size: Word);
                    Procedure SetDataTypeOfSet;
                    Procedure StoreStringConstantToCodeBlock;
                    Procedure StoreIntegerToStackFrame;
                    Procedure ConvertIntegerToExtended;
                    Procedure ConvertIntegerToReal;
                    Procedure ConvertRealToExtended;
                    Procedure ConvertExtendedToReal;
                    Procedure ConvertCharToString;
                    Procedure ConvertZeroBasedCharacterArrayToPChar;
                    Procedure CheckForConversionOfZeroBasedCharacterArrayToPChar (SecondExpressionTypeDef: PTypeDefinition);
                    Procedure CopyCharArrayCompatibleWithStringToStackFrameAsString;
                    Procedure ExpectStringExpression;
                    Procedure ExpectStringVariable;
                    Procedure ExpectIntegerOrFloatingPointExpression;
                    Procedure ExpectIntegerOrFloatingPointExpressionInParentheses;
                    Procedure ExpectIntegerExpression;
                    Procedure ExpectIntegerExpressionInParentheses;
                    Procedure Expect16BitIntegerVariable;
                    Procedure ExpectOrdinalExpression;
                    Procedure ExpectOrdinalExpressionInParentheses;
                    Procedure ExpectBooleanExpression;
                    Procedure ExpectPointerExpression;
                    Procedure ExpectPointerVariable;
                    Procedure ExpectVariableIdentifier;
                    Procedure ExpectVariableReference;
                    Procedure ExpectConstantExpression;
                    Procedure ExpectFileVariableAndPushPointerToMemory;
                    Procedure CheckOrdinalRange (TypePtr: PTypeDefinition);
                    Procedure CheckRange (TypeDef: PTypeDefinition);
                    Procedure CheckOrdinalOverflowAndStore (Int: LongInt; Overflow: Boolean);
                    Procedure ConvertToLowestFPTypeAndStoreToCodeBlock;
                    Procedure Calculate;
                    Procedure EndIntermediateCodeSubroutine;
                    Procedure SetExpressionToBooleanJump (OpCode: Word);
                    Procedure SetConstantBooleanExpression (BoolValue: Boolean);
                    Procedure ConvertBooleanJumpToByteValue;
                    Procedure ConvertToBooleanByte;
                    Procedure ConvertExpressionToBooleanJump;
                    Procedure CreateSpaceInStackFrameAndPushAddressToIt (Size: Word);
                    Procedure ExpectAssignableVariableReferenceExceptProcedureOrFunction;
                    Procedure ExpectAssignableVariableReferenceExceptProcAndFuncAndPushPointerToMemory;
                    Procedure GenerateSegmentOverride;
                    Function  ExpressionBaseType: TBaseType;
                    Function  ExpressionSegmentRegister: Byte;
                    Procedure GenerateInstructionWithSegment (OpCode: Byte);
                    Procedure GenerateOffsetReferenceForExpressionInMemory;
                    Procedure StoreExpressionConstantToCodeBlock (Data: Pointer; Size: Word);
                    Procedure LoadExpressionToRegister (Register: Byte);
                    Procedure LoadExpressionToRegisters (Register: TUsedRegister);
                    Procedure GenerateInstructionWithMemoryReference (MainOpCode, MODEM_OpCode: Byte);
                    Procedure GenerateInstructionWithExpressionInMemOrReg (MainOpCode, MODEM_OpCode: Byte);
                    Procedure Push;
                    Procedure LoadAddressOfLocalVariable (Flags: TVarFlagsSet; ProcIdentifier: Word);
                    Procedure LoadOffsetTo_DI;
                    Procedure PositionToOpenParameterHighestIndex;
                    Function  Load_DI_AndReturnSegment: Byte;
                    Procedure ConvertToPointerAndPush;
                    Procedure PushExpression;
                    Procedure LoadPointerToMemoryTo_DX_AX;
                    Procedure GenerateArithmeticInstructionWith_ACC (OpCode: Byte);
                    Procedure GenerateArithmeticInstructionWith_DX (OpCode: Byte);
                    Procedure GenerateArithmeticInstructionWithImmediateValue (ImmediateValue: Integer; SecondOpCode: Byte);
                    Procedure NOT_Integer;
                    Procedure IntegerMultiplicationWithConstant (Register: Byte; Factor: Integer);
                    Procedure Save (AlreadyUsedRegisters: TUsedRegistersSet);
                    Procedure PopToRegisters (AlreadyUsedRegisters: TUsedRegistersSet);
                    Procedure SwitchBetweenLoWordAndHiWord (Shift: ShortInt);
                    Function  ExpressionSegment: Byte;
                    Procedure LoadConstantSmallSetIntoValue;
                    Function  SetDataTypeForSimpleType: Boolean;
                    Procedure LoadSetAndPopPointer (TypeDefDataType: TIntegerTypeSet);
                    Procedure ExpandSetToStackFrameAndPushPointer (B: Byte);
                    Procedure GenerateFPUInstructionWithExpressionInMemory (MainOpCode, MODEM_OpCode: Byte);
                    Procedure PushRealExpression;
                    Procedure PushArrayPointerAndHighestIndex;
                    Procedure PushExpressionInMemory (Size: Word);
                    Procedure Push_FP_Expression (ParameterTypeDef: PTypeDefinition);
                    Procedure LoadSelfAddress;
                    Procedure ExpectVariableReferenceExceptProcedureOrFunction;
                    Procedure FindAndProcessMethodCall (TempPtr: Pointer; Flags: TProcedureFlagsSet; IntType: TIntegerTypeSet;
                                                                                            CompilerError: TCompilerError);
                    Procedure Process_NEW;
                    Procedure ExpectAndStoreStringAndPushPointer;
                    Procedure ExpectAndStoreStringCompatibleExpressionAndPushPointer;
                    Procedure CopyStringToStackFrame (Size: Word);
                    Procedure PositionToStringLength;
                    Function  IsExpressionInOverlaidCode: Boolean;
                    Procedure RemoveFunctionResultFromStack;
                    Procedure CheckForStringConversionToPChar (TypeDef: PTypeDefinition);
                    Procedure GenerateInstruction_MOV_Memory_Register (Register: Byte);
                    Procedure GenerateInstruction_16bit_MOV_Memory_Register (Register: Byte);
                    Procedure GenerateInstruction_8_16_bit (MainOpcode, SecondOpCode: Byte);
                    Procedure GenerateInstruction_MOV_Memory_Immediate (ImmediateValue: Word);
                    Procedure CallProcedure;
                    Procedure SetExpressionToFunctionResult;
                    Procedure LoadExpressionToFPU;
                    Procedure LoadRealExpressionToRegisterSet (RealRegistersSet: TRealRegistersSet);
                    Procedure CreatePointerToVariableReference;
                  end;
 
 
 
© 2017 Turbo Pascal | Privacy Policy