I have just added PDB file generation to the Freya compiler, to support debugging. Piece of cake! It has been extremely easy… so far.
- Mark the dynamic assembly with DebuggableAttribute, passing both the DisableOptimizations and Default constants to the attribute constructor.
- Create the dynamic module as "debuggable", passing true as the last parameter for AssemblyBuilder.DefineDynamicModule.
- Associate an ISymbolDocumentWriter to each source file in the project. Use the DefineDocument method from MethodBuilder for this task.
- Since every node in the Abstract Syntax Tree has a SourceRange field pointing to its source file, each AST node, including statement nodes, may access their corresponding ISymbolDocumentWriter instance.
- Code generation for statements is achieved by calling the Generate virtual method defined at the root AstStatement class. I just inserted a call to ILGenerator.MarkSequencePoint at the very beginning of the base Generate method. This Reflection.Emit method requires an ISymbolDocumentWriter as its first parameter, and a range inside the source file... and both things can be extracted from the SourceRange stored at the node.
- Additionally, local variables declared by the programmed must apply the SetLocalSymInfo method to their local variable builders, since IL code does not contain metadata about the names of local variables.
This is all what you need to load a Freya source file in Microsoft's CLR Debugger, as shown in this image:
You can find more information on this topic at Mike Stall's .NET Debugging blog. Of course, this is just a first step towards integrating the compiler in the Visual Studio 2005 IDE.