compiler: Implement init()
Этот коммит содержится в:
родитель
9a988dd1d1
коммит
c940617849
4 изменённых файлов: 19 добавлений и 2 удалений
|
@ -2,9 +2,11 @@
|
||||||
#include "runtime.h"
|
#include "runtime.h"
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
|
void go_init() __asm__("runtime.initAll");
|
||||||
void go_main() __asm__("main.main");
|
void go_main() __asm__("main.main");
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
|
go_init();
|
||||||
go_main();
|
go_main();
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
|
|
@ -18,7 +18,6 @@ void uart_send(uint8_t c) {
|
||||||
}
|
}
|
||||||
|
|
||||||
void _start() {
|
void _start() {
|
||||||
uart_init(6); // pin_tx = 6, for NRF52840-DK
|
|
||||||
main();
|
main();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -6,6 +6,10 @@ package runtime
|
||||||
// #include "runtime_nrf.h"
|
// #include "runtime_nrf.h"
|
||||||
import "C"
|
import "C"
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
C.uart_init(6) // pin_tx = 6, for NRF52840-DK
|
||||||
|
}
|
||||||
|
|
||||||
const Microsecond = 1
|
const Microsecond = 1
|
||||||
|
|
||||||
func putchar(c byte) {
|
func putchar(c byte) {
|
||||||
|
|
14
tgo.go
14
tgo.go
|
@ -55,6 +55,7 @@ type Compiler struct {
|
||||||
memsetIntrinsic llvm.Value
|
memsetIntrinsic llvm.Value
|
||||||
itfTypeNumbers map[types.Type]uint64
|
itfTypeNumbers map[types.Type]uint64
|
||||||
itfTypes []types.Type
|
itfTypes []types.Type
|
||||||
|
initFuncs []llvm.Value
|
||||||
}
|
}
|
||||||
|
|
||||||
type Frame struct {
|
type Frame struct {
|
||||||
|
@ -212,6 +213,17 @@ func (c *Compiler) Parse(mainPath string, buildTags []string) error {
|
||||||
c.parsePackage(program, pkg)
|
c.parsePackage(program, pkg)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// After all packages are imported, add a synthetic initializer function
|
||||||
|
// that calls the initializer of each package.
|
||||||
|
initType := llvm.FunctionType(llvm.VoidType(), nil, false)
|
||||||
|
initFn := llvm.AddFunction(c.mod, "runtime.initAll", initType)
|
||||||
|
block := c.ctx.AddBasicBlock(initFn, "entry")
|
||||||
|
c.builder.SetInsertPointAtEnd(block)
|
||||||
|
for _, fn := range c.initFuncs {
|
||||||
|
c.builder.CreateCall(fn, nil, "")
|
||||||
|
}
|
||||||
|
c.builder.CreateRetVoid()
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -434,7 +446,7 @@ func (c *Compiler) parseFuncDecl(f *ssa.Function) (*Frame, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Compiler) parseFunc(frame *Frame, f *ssa.Function) error {
|
func (c *Compiler) parseFunc(frame *Frame, f *ssa.Function) error {
|
||||||
if frame.llvmFn.Name() != "main.main" {
|
if frame.llvmFn.Name() != "main.main" && frame.llvmFn.Name() != "runtime.init" {
|
||||||
// This function is only used from within Go.
|
// This function is only used from within Go.
|
||||||
frame.llvmFn.SetLinkage(llvm.PrivateLinkage)
|
frame.llvmFn.SetLinkage(llvm.PrivateLinkage)
|
||||||
}
|
}
|
||||||
|
|
Загрузка…
Создание таблицы
Сослаться в новой задаче