Difference between revisions of "The parse tree/fr"
Line 270: | Line 270: | ||
''(dernière mise à jour pour fpc version 2.1.1, 2005-06-11)'' | ''(dernière mise à jour pour fpc version 2.1.1, 2005-06-11)'' | ||
− | + | Génération de code | |
{| border="1" cellpadding="3" cellspacing="0" | {| border="1" cellpadding="3" cellspacing="0" | ||
! style="background:#f0f0f0;text-align:left;" | TLocalSwitches | ! style="background:#f0f0f0;text-align:left;" | TLocalSwitches | ||
− | ! style="background:#f0f0f0;text-align:left;" | | + | ! style="background:#f0f0f0;text-align:left;" | Commutateur |
− | ! style="background:#f0f0f0;text-align:left;" | | + | ! style="background:#f0f0f0;text-align:left;" | Paramètre |
! style="background:#f0f0f0;text-align:left;" | Description | ! style="background:#f0f0f0;text-align:left;" | Description | ||
|- | |- | ||
− | | cs_check_overflow || {$Q+} || -Co || | + | | cs_check_overflow || {$Q+} || -Co || Le générateur de code devrait émettre du code de contrôle de débordement |
|- | |- | ||
− | | cs_check_range || {$R+} || -Cr, -CR || | + | | cs_check_range || {$R+} || -Cr, -CR || Le générateur de code devrait émettre du code de contrôle des intervalles |
|- | |- | ||
− | | cs_check_object || || -CR || | + | | cs_check_object || || -CR || Le générateur de code devrait émettre du code pour vérifier la validité des appels de méthode |
|- | |- | ||
− | | cs_check_io || {$I+} || -Ci || | + | | cs_check_io || {$I+} || -Ci || Le générateur de code devrait émettre du code de contrôle des E/S |
|- | |- | ||
− | | cs_check_stack || {$S+} || -Ct || | + | | cs_check_stack || {$S+} || -Ct || Le générateur de code devrait émettre du code de cvontrôle de la pile |
|- | |- | ||
− | | cs_checkpointer || || -gc || | + | | cs_checkpointer || || -gc || Le générateur de code devrait émettre du code de contrôle de pointeur |
|- | |- | ||
− | | cs_omitstackframe || N/A || || | + | | cs_omitstackframe || N/A || || Le générateur de code ne devrait pas émettre du code d'installation de frame_pointer (''frame_pointer setup'') dans le code d'entrée (''entry code'') (n'est pas utilisé dans le compilateur) |
|- | |- | ||
− | | cs_do_assertion || {$C+} || -Sa || | + | | cs_do_assertion || {$C+} || -Sa || Le générateur de code supporte l'utilisation de la routine en-ligne assert (''assert inline routine'') |
|- | |- | ||
− | | cs_generate_rtti || {$M+} || || | + | | cs_generate_rtti || {$M+} || || Le générateur de code devrait émettre de l'information de type à l'exécution (''runtime type information'') |
|- | |- | ||
− | | cs_full_boolean_eval || {$B+} || || | + | | cs_full_boolean_eval || {$B+} || || Mode d'évaluation des booléens |
|- | |- | ||
| cs_typed_const_writable || {$J+} || || '''''todo''''' | | cs_typed_const_writable || {$J+} || || '''''todo''''' | ||
Line 306: | Line 306: | ||
{| border="1" cellpadding="3" cellspacing="0" | {| border="1" cellpadding="3" cellspacing="0" | ||
! style="background:#f0f0f0;text-align:left;" | TLocalSwitches | ! style="background:#f0f0f0;text-align:left;" | TLocalSwitches | ||
− | ! style="background:#f0f0f0;text-align:left;" | | + | ! style="background:#f0f0f0;text-align:left;" | Commutateur |
− | ! style="background:#f0f0f0;text-align:left;" | | + | ! style="background:#f0f0f0;text-align:left;" | Paramètre |
! style="background:#f0f0f0;text-align:left;" | Description | ! style="background:#f0f0f0;text-align:left;" | Description | ||
|- | |- | ||
− | | cs_mmx || {$MMX+} || || | + | | cs_mmx || {$MMX+} || || Le générateur de code peut utiliser des commandes MMX |
|- | |- | ||
− | | cs_mmx_saturation || {$SATURATION+} || || | + | | cs_mmx_saturation || {$SATURATION+} || || Le générateur de code peut utiliser des opérations saturées (MMX) |
|} | |} | ||
Line 318: | Line 318: | ||
{| border="1" cellpadding="3" cellspacing="0" | {| border="1" cellpadding="3" cellspacing="0" | ||
! style="background:#f0f0f0;text-align:left;" | TLocalSwitches | ! style="background:#f0f0f0;text-align:left;" | TLocalSwitches | ||
− | ! style="background:#f0f0f0;text-align:left;" | | + | ! style="background:#f0f0f0;text-align:left;" | Commutateur |
− | ! style="background:#f0f0f0;text-align:left;" | | + | ! style="background:#f0f0f0;text-align:left;" | Paramètre |
! style="background:#f0f0f0;text-align:left;" | Description | ! style="background:#f0f0f0;text-align:left;" | Description | ||
|- | |- | ||
− | | cs_typed_addresses || {$T+} || || | + | | cs_typed_addresses || {$T+} || || L'analyseur émets des pointeurs typés utilisant l'opérateur @ |
|- | |- | ||
− | | cs_strict_var_strings || {$V+} || || String | + | | cs_strict_var_strings || {$V+} || || Les types String doivent être identiques (même longueur) pour être compatibles |
|- | |- | ||
− | | cs_ansistrings || {$H+} || -Sh || | + | | cs_ansistrings || {$H+} || -Sh || L'analyseur créée une ansistring quand un type de String non spécifié est déclaré au lieu de ShortString par défaut |
|} | |} | ||
Line 333: | Line 333: | ||
{| border="1" cellpadding="3" cellspacing="0" | {| border="1" cellpadding="3" cellspacing="0" | ||
! style="background:#f0f0f0;text-align:left;" | TLocalSwitches | ! style="background:#f0f0f0;text-align:left;" | TLocalSwitches | ||
− | ! style="background:#f0f0f0;text-align:left;" | | + | ! style="background:#f0f0f0;text-align:left;" | Commutateur |
− | ! style="background:#f0f0f0;text-align:left;" | | + | ! style="background:#f0f0f0;text-align:left;" | Paramètre |
! style="background:#f0f0f0;text-align:left;" | Description | ! style="background:#f0f0f0;text-align:left;" | Description | ||
|- | |- |
Revision as of 12:58, 19 December 2020
│
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 | Les opếrandes peuvent être échangées |
nf_swaped | TBinOp | Les opérandes sont échangées |
nf_error | Mis à TRUE s'il y a eu une erreur en analysant ce noeud | |
nf_pass1_done | général | |
nf_write | général | Le noeud est écrit vers |
nf_isproperty | général | TRUE si c'est une propriété |
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 | Aucun avertissement/conseil n'a été généré. |
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)
Génération de code
TLocalSwitches | Commutateur | Paramètre | Description |
---|---|---|---|
cs_check_overflow | {$Q+} | -Co | Le générateur de code devrait émettre du code de contrôle de débordement |
cs_check_range | {$R+} | -Cr, -CR | Le générateur de code devrait émettre du code de contrôle des intervalles |
cs_check_object | -CR | Le générateur de code devrait émettre du code pour vérifier la validité des appels de méthode | |
cs_check_io | {$I+} | -Ci | Le générateur de code devrait émettre du code de contrôle des E/S |
cs_check_stack | {$S+} | -Ct | Le générateur de code devrait émettre du code de cvontrôle de la pile |
cs_checkpointer | -gc | Le générateur de code devrait émettre du code de contrôle de pointeur | |
cs_omitstackframe | N/A | Le générateur de code ne devrait pas émettre du code d'installation de frame_pointer (frame_pointer setup) dans le code d'entrée (entry code) (n'est pas utilisé dans le compilateur) | |
cs_do_assertion | {$C+} | -Sa | Le générateur de code supporte l'utilisation de la routine en-ligne assert (assert inline routine) |
cs_generate_rtti | {$M+} | Le générateur de code devrait émettre de l'information de type à l'exécution (runtime type information) | |
cs_full_boolean_eval | {$B+} | Mode d'évaluation des booléens | |
cs_typed_const_writable | {$J+} | todo | |
cs_allow_enum_calc | todo |
MMX
TLocalSwitches | Commutateur | Paramètre | Description |
---|---|---|---|
cs_mmx | {$MMX+} | Le générateur de code peut utiliser des commandes MMX | |
cs_mmx_saturation | {$SATURATION+} | Le générateur de code peut utiliser des opérations saturées (MMX) |
Parser
TLocalSwitches | Commutateur | Paramètre | Description |
---|---|---|---|
cs_typed_addresses | {$T+} | L'analyseur émets des pointeurs typés utilisant l'opérateur @ | |
cs_strict_var_strings | {$V+} | Les types String doivent être identiques (même longueur) pour être compatibles | |
cs_ansistrings | {$H+} | -Sh | L'analyseur créée une ansistring quand un type de String non spécifié est déclaré au lieu de ShortString par défaut |
MACPAS specific
TLocalSwitches | Commutateur | Paramètre | 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
Field | Description |
---|---|
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