│
English (en) │
français (fr) │
Retour au contenu FPC internals
L'arbre syntaxique
Architecture
(dernière mise à jour pour fpc version 1.0.x)
L'arbre est la base du compilateur. Quand le compilateur analyse les instructions et les blocs de code, ils sont convertis en une représentation d'arbre. Cet représentation d' arbre est actuellement une liste doublement chaînée. Depuis cet arbre, la génération de code peut être facilement implémentée.
En supposant que vous avez la syntaxe Pascal suivante :
x := x * y + (6 shl x);
L'arbre de structure sera construit en mémoire, où chque cercle représente un élément (un noeud) dans l'arbre :
http://www.pjh2.de/fpc/CompilerInternalsFigure03.png
Types de noeud
(dernière mise à jour pour fpc version 2.1.1, 2005-06-11)
Les noeuds possible dans l'arbre sont (de type TNodeTyp):
Tree type definition
|
Description
|
emptynode |
Aucun node (retourne nil au chargement depuis le ppu)
|
addn |
Répresente l'opérateur +
|
muln |
Répresente l'opérateur *
|
subn |
Répresente l'opérateur -
|
divn |
Répresente l'opérateur div
|
symdifn |
Répresente l'opérateur ><
|
modn |
Répresente l'opérateur mod
|
assignn |
Répresente l'opérateur := (affectation)
|
loadn |
Répresente l'utilisation d'une variable
|
rangen |
Répresente un intervalle (i.e. 0..9)
|
ltn |
Répresente l'opérateur <
|
lten |
Répresente l'opérateur <=
|
gtn |
Répresente l'opérateur >
|
gten |
Répresente l'opérateur >=
|
equaln |
Répresente l'opérateur =
|
unequaln |
Répresente l'opérateur <>
|
inn |
Répresente l'opérateur in
|
orn |
Répresente l'opérateur or
|
xorn |
Répresente l'opérateur xor
|
shrn |
Répresente l'opérateur shr
|
shln |
Répresente l'opérateur shl
|
slashn |
Répresente l'opérateur /
|
andn |
Répresente l'opérateur and
|
subscriptn |
Répresente un champ dans un oibjet ou un enregistrement
|
derefn |
Répresente une déréférence de pointeur (telle qu'avec l'opérateur ^)
|
addrn |
Répresente l'opérateur @
|
ordconstn |
Représente une constante ordinale
|
typeconvn |
Représente une conversion de type
|
calln |
Représente un appel de routine
|
callparan |
Représente un paramètre passé à une routine
|
realconstn |
Représente une constante en virgule flottante
|
unaryminusn |
Représente un signe négatif (p.ex. : -)
|
asmn |
Représente un noeud d'instruction assembleur
|
vecn |
Représente l'indexation d'un tableau
|
pointerconstn |
Représente une constante de type pointer
|
stringconstn |
Représente une constante de type string
|
notn |
Répresente l'opérateur not
|
inlinen |
Répresente une des routines internes (writeln,ord,etc.)
|
niln |
Représente le pointeur nil
|
errorn |
Représente une erreur dans l'analyse de ce noeud (utilisé pour la détection d'erreur et la correction)
|
typen |
Représente un nom de type (i.e typeof(obj))
|
setelementn |
Représente des ensembles d'éléments (i.e : [a..b], [a,b,c]) (non constant)
|
setconstn |
Représente un ensemble d'éléments constants i.e : [1..9], [1,2,3])
|
blockn |
Représente un bloc d'instructions
|
statementn |
Représente une instruction dans un bloc de noeuds
|
ifn |
Représente une instruction if
|
breakn |
Représente une instruction break
|
continuen |
Représente une instruction continue
|
whilerepeatn |
Représente une instruction while ou repeat
|
forn |
Représente une instruction for
|
exitn |
Représente une instruction exit
|
withn |
Représente une instruction with
|
casen |
Représente une instruction case
|
labeln |
Représente une instruction label
|
goton |
Représente une instruction goto
|
tryexceptn |
Représente une instruction try..except
|
raisen |
Représente une instruction raise
|
tryfinallyn |
Représente une instruction try..finally
|
onn |
Représente une instruction on..do (dans le code de traitement d'une exception)
|
isn |
Répresente l'opérateur is
|
asn |
Répresente l'opérateur de conversion de type as
|
caretn |
Répresente l'opérateur ^
|
starstarn |
Répresente l'opérateur ** (exponentiation)
|
arrayconstructorn |
Répresente un noeud de construction pour l'analyse de for [...]
|
arrayconstructorrangen |
Elément d'intervalle pour permettre aux ensembles dans l'arbre de construction d'un tableautree
|
tempcreaten |
pour les temporaires dans le result/firstpass
|
temprefn |
Références vers temporaires.
|
tempdeleten |
Pour les temporaires dans le result/firstpass
|
addoptn |
Ajouté pour les optimisations où nous ne pouvons pas supprimer.
|
nothingn |
NOP, Ne fait rien
|
loadvmtaddrn |
Charge l'adresse de la VMT d'une classe/d'un objet
|
guidconstn |
Une constante GUID d'interface COM
|
rttin |
Informations Rtti pour qu'elles soient accessibles dans result/firstpass
|
loadparentfpn |
Charge le framepointer du parent pour les procédures imbriquées.
|
Champs de structure de noeud (node.pas)
(dernière mise à jour pour fpc version fpc version 2.1.1, 2005-06-11)
type
TNode = class
public
NodeType: TNodeType; // type of this node
BlockType: TBlock_Type; // type of the current code block, general/const/type
ExpectLoc: TCGLoc; // expected location of the result of this node (pass1)
Location: TLocation; // the location of the result of this node (pass2)
Parent: TNode; // the parent node of this is node
// this field is set by concattolist
Flags: TNodeFlags; // there are some properties about the node stored
PpuIdx: Longint;
RegistersInt, // the number of registers needed to evalute the node
RegistersFpu,
RegistersMm: Longint; // must be longint !!!!
{$ifdef SUPPORT_MMX}
RegistersMmx: Longint;
{$endif SUPPORT_MMX}
ResultType: TType;
FileInfo: TFilePosInfo;
LocalSwitches: TLocalSwitches;
{$ifdef extdebug}
MaxFirstPassCount,
FirstPassCount: Longint;
{$endif extdebug}
end;
TNodeFlag
(dernière mise à jour pour fpc version 2.1.1, 2005-06-11)
TNodeType
|
|
Description
|
nf_swapable |
TBinOp |
operands can be swaped
|
nf_swaped |
TBinOp |
operands are swaped
|
nf_error |
|
Set to TRUE if there was an error parsing this node
|
nf_pass1_done |
general |
|
nf_write |
general |
Node is written to
|
nf_isproperty |
general |
TRUE if this is a property
|
nf_typedaddr |
TAddrNode |
|
nf_no_checkpointer |
TDerefNode |
|
nf_memindex |
TVecNode |
|
nf_memseg |
TVecNode |
|
nf_callunique |
TVecNode |
|
nf_absolute |
TLoadNode |
|
nf_is_self |
TLoadNode |
|
nf_load_self_pointer |
TLoadNode |
|
nf_is_currency |
TAddNode |
|
nf_has_pointerdiv |
TAddNode |
|
nf_concat_string |
TAssignmentNode |
|
nf_use_strconcat |
TAssignmentNode |
|
nf_forcevaria |
TArrayConstructNode |
|
nf_novariaallowed |
TArrayConstructNode |
|
nf_explicit |
TTypeConvNode |
|
nf_internal |
TTypeConvNode |
no warnings/hints generated
|
nf_load_procvar |
TTypeConvNode |
|
nf_inlineconst |
TInlineNode |
|
nf_get_asm_position |
TAsmNode |
|
nf_block_with_exit |
TBlockNode |
|
TLocalSwitches
(dernière mise à jour pour fpc version 2.1.1, 2005-06-11)
Code generation
TLocalSwitches
|
Switch
|
Param
|
Description
|
cs_check_overflow |
{$Q+} |
-Co |
Code generator should emit overflow checking code
|
cs_check_range |
{$R+} |
-Cr, -CR |
Code generator should emit range checking code
|
cs_check_object |
|
-CR |
Code generator should emit code to verify object method call validity
|
cs_check_io |
{$I+} |
-Ci |
Code generator should emit I/O checking code
|
cs_check_stack |
{$S+} |
-Ct |
Code generator should emit stack checking code
|
cs_checkpointer |
|
-gc |
Code generator should emit pointer checking code
|
cs_omitstackframe |
N/A |
|
Code generator should not emit frame_pointer setup code in entry code (don't used in the compiler)
|
cs_do_assertion |
{$C+} |
-Sa |
Code generator supports using the assert inline routine
|
cs_generate_rtti |
{$M+} |
|
Code generator should emit runtime type information
|
cs_full_boolean_eval |
{$B+} |
|
Boolean evalution mode
|
cs_typed_const_writable |
{$J+} |
|
todo
|
cs_allow_enum_calc |
|
|
todo
|
MMX
TLocalSwitches
|
Switch
|
Param
|
Description
|
cs_mmx |
{$MMX+} |
|
Code generator can use MMX commands
|
cs_mmx_saturation |
{$SATURATION+} |
|
Code generator can use saturated operations (MMX)
|
Parser
TLocalSwitches
|
Switch
|
Param
|
Description
|
cs_typed_addresses |
{$T+} |
|
Parser emits typed pointer using the @ operator
|
cs_strict_var_strings |
{$V+} |
|
String types must be identical (same length) to be compatible
|
cs_ansistrings |
{$H+} |
-Sh |
Parser creates an ansistring when an unspecified String type is declared instead of the default ShortString
|
MACPAS specific
TLocalSwitches
|
Switch
|
Param
|
Description
|
cs_external_var |
{$J+} |
|
todo
|
cs_externally_visible |
{$Z+} |
|
todo
|
Additional fields
(dernière mise à jour pour fpc version 1.0.x)
Depending on the tree type, some additional fields may be present in the tree node. This section describes these additional fields. Before accessing these additional fields, a check on the treetype should always be done to verify if not reading invalid memory ranges.
AddN
Field
|
Description
|
Use_StrConcat: Boolean; |
Currently unused (use for optimizations in future versions)
|
String_Typ: TStringType; |
In the case where the + operator is applied on a string, this field indicates the string type.
|
CallParaN
Field
|
Description
|
Is_Colon_Para : Boolean; |
Used for internal routines which can use optional format parameters (using colons). Is set to TRUE if this parameter was preceded by a colon (i.e : :1)
|
Exact_Match_Found : Boolean; |
Set to TRUE if the parameter type is exactly the same as the one expected by the routine.
|
ConvLevel1Found : Boolean; |
Set to TRUE if the parameter type requires a level 1 type conversion to conform to the parameter expected by the routine.
|
ConvLevel2Found : Boolean; |
Set to TRUE if the parameter type requires a level 2 type conversion to conform to the parameter expected by the routine.
|
HighTree : pTree; |
|
AssignN
Field
|
Description
|
AssignTyp: TAssignTyp; |
Currently unused (Used to be used for C-like assigns)
|
Concat_String: Boolean; |
Currently unused (use for optimizations in future versions)
|
LoadN
Field
|
Description
|
SymTableEntry: PSym; |
Symbol table entry for this symbol
|
SymTable: PSymTable; |
Symbol table in which this symbol is stored
|
Is_Absolute: Boolean; |
set to TRUE if this variable is absolute
|
Is_First: Boolean; |
set to TRUE if this is the first occurrence of the load for this variable (used with the varstate variable for optimizations)
|
CallN
Field
|
Description
|
SymTableProcEntry: PProcSym; |
Symbol table entry for this routine
|
SymTableProc: PSymTable; |
Symbol table associated with a call (object symbol table or routine symbol table)
|
ProcDefinition: pAbstractProcDef; |
Type definition for this routine
|
MethodPointer: pTree; |
?????????
|
No_Check: Boolean; |
Currently unused
|
Unit_Specific: Boolean; |
set to TRUE if the routine is imported in a unit specific way (for example: system.writeln())
|
Return_Value_Used : Boolean |
set to TRUE if the routine is a function and that the return value is not used (in extended syntax parsing - $X+)
|
Static_Call: Boolean; |
unused
|
addrn
Field
|
Description
|
ProcVarLoad: Boolean; |
Set to TRUE if this is a procedural variable call
|
OrdConstN
Field
|
Description
|
Value: LongInt; |
The numeric value of this constant node
|
RealConstN
Field
|
Description
|
Value_Real: Best_Real; |
The numeric value of this constant node
|
Lab_Real: PAsmLabel; |
The assembler label reference to this constant
|
FixConstN
Field
|
Description
|
Value_Fix: LongInt; |
The numeric value of this constant node
|
FuncRetN
Field
|
Description
|
FuncRetProcInfo: Pointer; (PProcInfo) |
Pointer to procedure information
|
RetType: TType; |
Indicates the return type of the function
|
Is_First_FuncRet: Boolean; |
|
SubscriptN
Field
|
Description
|
vs: pVarSym; |
Symbol table entry for this variable (a field of object/class/record)
|
RaiseN
Field
|
Description
|
FrameTree: PTree; |
Exception frame tree (code in Raise statement)
|
VecN
Field
|
Description
|
MemIndex: Boolean; |
Set to TRUE if Mem[Seg:Ofs] directive is parsed
|
MemSeg: Boolean; |
Set to TRUE if Mem[Seg:Ofs] directive is parsed
|
CallUnique: Boolean; |
|
StringConstN
Field
|
Description
|
Value_Str: PChar; |
The constant value of the string
|
Length: LongInt; |
Length of the string in bytes (or in characters???)
|
Lab_Str: PAsmLabel; |
The assembler label reference to this constant
|
StringType: TStringType; |
The string type (short, long, ansi, wide)
|
TypeConvN
Field
|
Description
|
ConvType: TConvertType; |
Indicates the conversion type to do
|
Explizit: Boolean; |
set to TRUE if this was an explicit conversion (with explicit typecast, or calling one of the internal conversion routines)
|
TypeN
Field
|
Description
|
TypeNodeType: PDef; |
The type definition for this node
|
TypeNodeSym: PTypeSym; |
The type symbol information
|
InlineN
Field
|
Description
|
InlineNumber: Byte; |
Indicates the internal routine called (Cf. code generator)
|
InlineConst: Boolean; |
One or more of the parameters to this inline routine call contains constant values
|
ProcInlineN
Inline nodes are created when a routine is declared as being inline. The routine is actually inlined when the following conditions are satisfied:
It is called within the same module
The appropriate compiler switch to support inline is activated
It is a non-method routine (a standard procedure or function)
Otherwise a normal call is made, ignoring the inline directive. In the case where a routine is inlined, all parameters, return values and local variables of the inlined routine are actually allocated in the stack space of the routine which called the inline routine.
Field
|
Description
|
InlineTree: PTree; |
The complete tree for this inline procedure
|
InlineProcsym: PProcSym; |
Symbol table entry for this procedure
|
RetOffset: LongInt; |
Return offset in parent routine stack space
|
Para_Offset: LongInt; |
Parameter start offset in parent routine stack space
|
Para_Size: LongInt; |
Parameter size in the parent routine stack space
|
SetConstN
Field
|
Description
|
Value_Set: PConstSet; |
The numeric value of this constant node
|
Lab_Set: PAsmLabel; |
The assembler label reference to this constant
|
LoopN
AsmN
Field
|
Description
|
p_Asm: PAasmOutput; |
The instruction tree created by the assembler parser
|
Object_Preserved: Boolean; |
set to FALSE if the Self_Register was modified in the asm statement.
|
CaseN
Field
|
Description
|
Nodes: PCaseRecord; |
Tree for each of the possible case in the case statement
|
ElseBlock: PTree; |
Else statement block tree
|
LabelN, GotoN
Field
|
Description
|
LabelNr: PAsmLabel; |
Assembler label associated with this statement
|
ExceptionBlock: PTree; |
?
|
LabSym: PLabelSym; |
Symbol table entry for this label
|
WithN
Field
|
Description
|
WithSymTables: PWithSymTable; |
|
TableCount: LongInt; |
|
WithReference: PReference; |
|
IsLocal: Boolean; |
|
OnN
Field
|
Description
|
ExceptSymTable: PSymTable; |
|
ExceptType: PObjectDef; |
|
ArrayConstructorN
Field
|
Description
|
CArgs: Boolean; |
|
CArgSwap: Boolean; |
|
ForceVaria: Boolean; |
|
NoVariaAllowed: Boolean; |
|
ConstructorDef: PDef; |
|
Next chapter: Symbol tables