@@ -85,6 +85,43 @@ continuation, storing all state needed to continue traversal at the type members
85
85
been registered with the cache. (This implementation approach might be a tad over-engineered and
86
86
may change in the future)
87
87
88
+
89
+ ## Source Locations and Line Information
90
+ In addition to data type descriptions the debugging information must also allow to map machine code
91
+ locations back to source code locations in order to be useful. This functionality is also handled in
92
+ this module. The following functions allow to control source mappings:
93
+
94
+ + set_source_location()
95
+ + clear_source_location()
96
+ + start_emitting_source_locations()
97
+
98
+ `set_source_location()` allows to set the current source location. All IR instructions created after
99
+ a call to this function will be linked to the given source location, until another location is
100
+ specified with `set_source_location()` or the source location is cleared with
101
+ `clear_source_location()`. In the later case, subsequent IR instruction will not be linked to any
102
+ source location. As you can see, this is a stateful API (mimicking the one in LLVM), so be careful
103
+ with source locations set by previous calls. It's probably best to not rely on any specific state
104
+ being present at a given point in code.
105
+
106
+ One topic that deserves some extra attention is *function prologues*. At the beginning of a
107
+ function's machine code there are typically a few instructions for loading argument values into
108
+ allocas and checking if there's enough stack space for the function to execute. This *prologue* is
109
+ not visible in the source code and LLVM puts a special PROLOGUE END marker into the line table at
110
+ the first non-prologue instruction of the function. In order to find out where the prologue ends,
111
+ LLVM looks for the first instruction in the function body that is linked to a source location. So,
112
+ when generating prologue instructions we have to make sure that we don't emit source location
113
+ information until the 'real' function body begins. For this reason, source location emission is
114
+ disabled by default for any new function being translated and is only activated after a call to the
115
+ third function from the list above, `start_emitting_source_locations()`. This function should be
116
+ called right before regularly starting to translate the top-level block of the given function.
117
+
118
+ There is one exception to the above rule: `llvm.dbg.declare` instruction must be linked to the
119
+ source location of the variable being declared. For function parameters these `llvm.dbg.declare`
120
+ instructions typically occur in the middle of the prologue, however, they are ignored by LLVM's
121
+ prologue detection. The `create_argument_metadata()` and related functions take care of linking the
122
+ `llvm.dbg.declare` instructions to the correct source locations even while source location emission
123
+ is still disabled, so there is no need to do anything special with source location handling here.
124
+
88
125
*/
89
126
90
127
0 commit comments