/
One-pass structure definition One-pass structure definition

One-pass structure definition - PowerPoint Presentation

briana-ranney
briana-ranney . @briana-ranney
Follow
344 views
Uploaded On 2020-01-30

One-pass structure definition - PPT Presentation

Onepass structure definition must occur before any uses that is uses can have only backward references to definitions examples macro definition before macro calls variable ID: 774237

Share:

Link:

Embed:

Download Presentation from below link

Download Presentation The PPT/PDF document "One-pass structure definition" is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.


Presentation Transcript

One-pass structure definition must occur before any uses (that is, uses can have only backward references to definitions ) examples: macro definition before macro calls variable declarations before uses

Two- p ass structure uses can have forward or backward references to definitions first pass collects the definitions into a table (e.g., symbol table) second pass accomplishes the translation by consulting the definition table whenever a use is encountered only the table is required to remain in memory; the source file can be read from disk twice example : traditional assembler

One-pass with fixup structure also known as back-patching the translated code as well as the definition table both need to be kept in memory algorithm for assembler label(x) // x defined at location 10 ... blt0(y ) // y used at location 15 ... beq0(y ) // y used at location 20 ... label(y) // y defined at location 25

One-pass with fixup structure In one-pass-with-fixup, the symbol table needs a third field ( in addition to the symbol name and address fields). The third field indicates whether the symbol is defined or undefined.

One-pass with fixup structure When you encounter the use of a symbol, perform a lookup in the symbol table: if the symbol is defined, use the address value from the entry

One-pass with fixup structure . . . x def 10 . . . y undef <head> . . . 15 NULL use-location node if the symbol is not yet present, add an entry for this symbol and mark it as undefined; use the address field in the entry as a pointer to a linked list of use locations for this undefined symbol and add the address of the current word to the first node , e.g.

One-pass with fixup structure if the symbol is present but undefined, add another node to the linked list with the address of the current word in that node , e.g., . . . y undef <head> 20 <next> 15 NULL . . . Note that you can extend the use-location nodes to contain an indicator for what type of fixup is needed, e.g., full-word value , high 22 bits, low 10 bits, etc.

One-pass with fixup When you encounter the definition of a symbol, perform a lookup in the symbol table: if the symbol is already defined, you have encountered a "multiple definition" error if the symbol is not yet present, add an entry for this symbol and mark it as defined; use the current value of the location counter as the address

One-pass with fixup . . . x def 10 . . . y def 25 . . . if the symbol is present but undefined, "fix up" or "back patch " all the undefined uses by traversing the linked list and storing the current value of the location counter into the memory words identified in the nodes of the list; then free the list and mark the symbol as defined using the current value of the location counter as the address, e.g., symbol type address

One-pass with fixup When you reach the end of the assembly language program, scan the symbol table for any remaining undefined entries. These are either errors or, absent a requirement to explicitly mark the use of external names, these are symbols that must be passed to the linker to be resolved . To visualize this, consider the actions of the assembler as it moves through a possible source program:

One-pass with fixup start translation ... ... use x ... // forward reference - insert "x" into the // symbol table as an undefined symbol and // start a linked list of use locations ... ... use x ... // forward reference - add this use location // to the linked list ... ... define x ... // traverse the linked list and fixup all // forward references with the value of the // definition, then mark "x" as a defined // symbol and store the defining value ... ... use x ... // backward reference - resolve by table lookup ... ... define x ... // multiply-defined symbol error! ... end translation // check for undefined symbols

Numeric local labels in a two-pass structure Numeric local labels in a two-pass structure (a relaxation of the prohibition on multiply-defined symbols) (adapted from the GNU description) Local labels help programmers use names temporarily. They are numeric symbols that can be redefined at various points in a program, but each valid use of a local label has a unique definition to which it refers . To define a local label for our chapter 1 assemblers, write a label with a number <N> label(<N>)

Numeric local labels in a two-pass structure label (<N>) To refer to the most recent previous definition of that symbol write <N>b, using the same number as when you defined the label. To refer to the next definition of a local label, write <N>f. The "b" stands for "backward", and the "f" stands for "forward". Prior to the first definition of label <N>, the reference <N>b is undefined; likewise , after the last definition of label <N>, the reference <N>f is undefined.

Numeric local labels in a two-pass structure Prior to the first definition of label <N>, the reference <N>b is undefined; likewise , After the last definition of label <N>, the reference <N>f is undefined . Local labels are useful in the bodies of macro definitions so that you do not need macro expansion counters to generate unique labels.

Numeric local labels in a two-pass structure Consider the following example code with two definitions of label "1": assembly code location generated code and data label(start ) ba (1f ) < address 0> 70 2 label(1 ) ba(1f) <address 2> 70 4 label(1) ba(x) <address 4> 70 8 label(1) ba (start) <address 6> 70 0 label(x) halt <address 8> 0 end(1b) <address 9> 6Note that in the first branch the target address "1f" is resolved to 2, but in the second branch the target address "1f" is resolved to 4.

Numeric local labels in a two-pass structure For the above assembly code, the first pass of a two-pass assembler builds a symbol table with a local/global flag and a linked list of definitions from each local entry: 1 local * undef * 2 * 4 * 6 * undef * start global 0 = x global 8 The three definitions of the local label "1" in the example code above are recorded in the linked list in the symbol table with the locations 2 , 4, and 6, respectively, along with header and trailer nodes that specify that the location is undefined.

Numeric local labels in a two-pass structure During the second pass, the assembler removes the head node from a local label's list whenever a redefinition of that local label is encountered. Thus , because there are three definitions of "1" in the above code, by the end of the second pass only two nodes (the location-6 node and the undefined-trailer node) remain in the linked list for "1". With the removal of the head node when encountering a (re-)definition, the rules for resolving a local label "1b" of "1f" during the second pass are: "1b" - use the value from the first node in the linked list for "1" "1f" - use the value from the second node in the linked list for "1"

Numeric local labels in a two-pass structure To visualize this, consider how the linked list would be modified as the assembler moves through the example source code on the second pass : /* at beginning of second pass, "1" has list (undef,2,4,6,undef ) */ 0: label(start) ba (1f) 70 2 // "1f" resolves to 2 2: label(1) // redefine "1", so "1" now // has list (2,4,6,undef) ba (1f) 70 4 // "1f" resolves to 4 4: label(1) // redefine "1", so "1" now // has list (4,6,undef) ba(x) 70 8 6: label(1) // redefine "1", so "1" now // has list (6,undef) ba (start) 70 0 8: label(x) halt 0 9: end(1b) 6 // "1b" resolves to 6