MCPcopy
hub / github.com/tinygo-org/tinygo / CompilePackage

Function CompilePackage

compiler/compiler.go:291–371  ·  view source on GitHub ↗

CompilePackage compiles a single package to a LLVM module.

(moduleName string, pkg *loader.Package, ssaPkg *ssa.Package, machine llvm.TargetMachine, config *Config, dumpSSA bool)

Source from the content-addressed store, hash-verified

289
290// CompilePackage compiles a single package to a LLVM module.
291func CompilePackage(moduleName string, pkg *loader.Package, ssaPkg *ssa.Package, machine llvm.TargetMachine, config *Config, dumpSSA bool) (llvm.Module, []error) {
292 c := newCompilerContext(moduleName, machine, config, dumpSSA)
293 defer c.dispose()
294 c.packageDir = pkg.OriginalDir()
295 c.embedGlobals = pkg.EmbedGlobals
296 c.pkg = pkg.Pkg
297 c.runtimePkg = ssaPkg.Prog.ImportedPackage("runtime").Pkg
298 c.program = ssaPkg.Prog
299
300 // Convert AST to SSA.
301 ssaPkg.Build()
302
303 // Initialize debug information.
304 if c.Debug {
305 c.cu = c.dibuilder.CreateCompileUnit(llvm.DICompileUnit{
306 Language: 0xb, // DW_LANG_C99 (0xc, off-by-one?)
307 File: "<unknown>",
308 Dir: "",
309 Producer: "TinyGo",
310 Optimized: true,
311 })
312 }
313
314 // Load comments such as //go:extern on globals.
315 c.loadASTComments(pkg)
316
317 // Predeclare the runtime.alloc function, which is used by the wordpack
318 // functionality.
319 c.getFunction(c.program.ImportedPackage("runtime").Members["alloc"].(*ssa.Function))
320 if c.NeedsStackObjects {
321 // Predeclare trackPointer, which is used everywhere we use runtime.alloc.
322 c.getFunction(c.program.ImportedPackage("runtime").Members["trackPointer"].(*ssa.Function))
323 }
324
325 // Compile all functions, methods, and global variables in this package.
326 irbuilder := c.ctx.NewBuilder()
327 defer irbuilder.Dispose()
328 c.createPackage(irbuilder, ssaPkg)
329
330 // see: https://reviews.llvm.org/D18355
331 if c.Debug {
332 c.mod.AddNamedMetadataOperand("llvm.module.flags",
333 c.ctx.MDNode([]llvm.Metadata{
334 llvm.ConstInt(c.ctx.Int32Type(), 2, false).ConstantAsMetadata(), // Warning on mismatch
335 c.ctx.MDString("Debug Info Version"),
336 llvm.ConstInt(c.ctx.Int32Type(), 3, false).ConstantAsMetadata(), // DWARF version
337 }),
338 )
339 c.mod.AddNamedMetadataOperand("llvm.module.flags",
340 c.ctx.MDNode([]llvm.Metadata{
341 llvm.ConstInt(c.ctx.Int32Type(), 7, false).ConstantAsMetadata(), // Max on mismatch
342 c.ctx.MDString("Dwarf Version"),
343 llvm.ConstInt(c.ctx.Int32Type(), 4, false).ConstantAsMetadata(),
344 }),
345 )
346 if c.TinyGoVersion != "" {
347 // It is necessary to set llvm.ident, otherwise debugging on MacOS
348 // won't work.

Callers 3

BuildFunction · 0.92
compileGoFileForTestingFunction · 0.92
testCompilePackageFunction · 0.85

Calls 6

newCompilerContextFunction · 0.85
OriginalDirMethod · 0.80
loadASTCommentsMethod · 0.80
createPackageMethod · 0.80
disposeMethod · 0.45
getFunctionMethod · 0.45

Tested by 2

compileGoFileForTestingFunction · 0.74
testCompilePackageFunction · 0.68