diff --git a/analysis.go b/analysis.go index dfda38bd..e60f7ed9 100644 --- a/analysis.go +++ b/analysis.go @@ -1,9 +1,7 @@ - package main import ( "go/types" - "golang.org/x/tools/go/ssa" ) diff --git a/src/device/arm/arm.go b/src/device/arm/arm.go index ff6f4c53..5a64c357 100644 --- a/src/device/arm/arm.go +++ b/src/device/arm/arm.go @@ -33,6 +33,7 @@ type __reg uint32 type RegValue = __reg type __asm string + func Asm(s __asm) const ( diff --git a/src/examples/blinky/blinky.go b/src/examples/blinky/blinky.go index b282806e..1479725b 100644 --- a/src/examples/blinky/blinky.go +++ b/src/examples/blinky/blinky.go @@ -1,4 +1,3 @@ - package main import ( diff --git a/src/examples/hello/hello.go b/src/examples/hello/hello.go index a860b4e6..f8389da7 100644 --- a/src/examples/hello/hello.go +++ b/src/examples/hello/hello.go @@ -1,4 +1,3 @@ - package main type Thing struct { @@ -84,7 +83,7 @@ func fib(n int) int { if n <= 2 { return 1 } - return fib(n - 1) + fib(n - 2) + return fib(n-1) + fib(n-2) } func sumrange(n int) int { diff --git a/src/machine/machine.go b/src/machine/machine.go index 91df6284..abe43688 100644 --- a/src/machine/machine.go +++ b/src/machine/machine.go @@ -1,4 +1,3 @@ - package machine type GPIOConfig struct { diff --git a/src/machine/machine_avr.go b/src/machine/machine_avr.go index 664f8399..23222ca3 100644 --- a/src/machine/machine_avr.go +++ b/src/machine/machine_avr.go @@ -1,4 +1,3 @@ - // +build avr package machine @@ -10,7 +9,7 @@ import ( type GPIOMode uint8 const ( - GPIO_INPUT = iota + GPIO_INPUT = iota GPIO_OUTPUT ) diff --git a/src/machine/machine_dummy.go b/src/machine/machine_dummy.go index 8f355f6c..7c2f986b 100644 --- a/src/machine/machine_dummy.go +++ b/src/machine/machine_dummy.go @@ -1,4 +1,3 @@ - // +build !avr,!nrf package machine @@ -8,7 +7,7 @@ package machine type GPIOMode uint8 const ( - GPIO_INPUT = iota + GPIO_INPUT = iota GPIO_OUTPUT ) diff --git a/src/machine/machine_nrf.go b/src/machine/machine_nrf.go index 9f622d84..d1197f5e 100644 --- a/src/machine/machine_nrf.go +++ b/src/machine/machine_nrf.go @@ -1,4 +1,3 @@ - // +build nrf package machine diff --git a/src/runtime/gc.go b/src/runtime/gc.go index 7596af82..6bb06a75 100644 --- a/src/runtime/gc.go +++ b/src/runtime/gc.go @@ -1,4 +1,3 @@ - // +build !linux package runtime diff --git a/src/runtime/print.go b/src/runtime/print.go index bb606827..fe119852 100644 --- a/src/runtime/print.go +++ b/src/runtime/print.go @@ -1,4 +1,3 @@ - package runtime import ( @@ -96,8 +95,8 @@ func printitf(msg interface{}) { func printptr(ptr uintptr) { putchar('0') putchar('x') - for i := 0; i < int(unsafe.Sizeof(ptr)) * 2; i++ { - nibble := byte(ptr >> (unsafe.Sizeof(ptr) * 8 - 4)) + for i := 0; i < int(unsafe.Sizeof(ptr))*2; i++ { + nibble := byte(ptr >> (unsafe.Sizeof(ptr)*8 - 4)) if nibble < 10 { putchar(nibble + '0') } else { diff --git a/src/runtime/runtime.go b/src/runtime/runtime.go index 81c73560..228b1ede 100644 --- a/src/runtime/runtime.go +++ b/src/runtime/runtime.go @@ -1,4 +1,3 @@ - package runtime const Compiler = "tgo" diff --git a/src/runtime/runtime_avr.go b/src/runtime/runtime_avr.go index 939595fb..91e517cb 100644 --- a/src/runtime/runtime_avr.go +++ b/src/runtime/runtime_avr.go @@ -1,4 +1,3 @@ - // +build avr package runtime diff --git a/src/runtime/runtime_nrf.go b/src/runtime/runtime_nrf.go index 0b62b498..0250113a 100644 --- a/src/runtime/runtime_nrf.go +++ b/src/runtime/runtime_nrf.go @@ -1,4 +1,3 @@ - // +build nrf package runtime @@ -20,16 +19,17 @@ func init() { } func initUART() { - nrf.UART0.ENABLE = nrf.UART0_ENABLE_ENABLE_Enabled - nrf.UART0.BAUDRATE = nrf.UART0_BAUDRATE_BAUDRATE_Baud115200 + nrf.UART0.ENABLE = nrf.UART0_ENABLE_ENABLE_Enabled + nrf.UART0.BAUDRATE = nrf.UART0_BAUDRATE_BAUDRATE_Baud115200 nrf.UART0.TASKS_STARTTX = 1 - nrf.UART0.PSELTXD = 6 // pin 6 for NRF52840-DK + nrf.UART0.PSELTXD = 6 // pin 6 for NRF52840-DK } func initLFCLK() { nrf.CLOCK.LFCLKSRC = nrf.CLOCK_LFCLKSTAT_SRC_Xtal nrf.CLOCK.TASKS_LFCLKSTART = 1 - for nrf.CLOCK.EVENTS_LFCLKSTARTED == 0 {} + for nrf.CLOCK.EVENTS_LFCLKSTARTED == 0 { + } nrf.CLOCK.EVENTS_LFCLKSTARTED = 0 } @@ -41,16 +41,17 @@ func initRTC() { func putchar(c byte) { nrf.UART0.TXD = nrf.RegValue(c) - for nrf.UART0.EVENTS_TXDRDY == 0 {} + for nrf.UART0.EVENTS_TXDRDY == 0 { + } nrf.UART0.EVENTS_TXDRDY = 0 } func sleep(d Duration) { ticks64 := d / 32 for ticks64 != 0 { - monotime() // update timestamp + monotime() // update timestamp ticks := uint32(ticks64) & 0x7fffff // 23 bits (to be on the safe side) - C.rtc_sleep(C.uint32_t(ticks)) // TODO: not accurate (must be d / 30.5175...) + C.rtc_sleep(C.uint32_t(ticks)) // TODO: not accurate (must be d / 30.5175...) ticks64 -= Duration(ticks) } } diff --git a/src/runtime/runtime_unix.go b/src/runtime/runtime_unix.go index 1cb8256a..aedb197a 100644 --- a/src/runtime/runtime_unix.go +++ b/src/runtime/runtime_unix.go @@ -1,4 +1,3 @@ - // +build linux package runtime @@ -30,7 +29,7 @@ func sleep(d Duration) { func monotime() uint64 { var ts C.struct_timespec C.clock_gettime(C.CLOCK_MONOTONIC, &ts) - return uint64(ts.tv_sec) * 1000 * 1000 + uint64(ts.tv_nsec) / 1000 + return uint64(ts.tv_sec)*1000*1000 + uint64(ts.tv_nsec)/1000 } func abort() { diff --git a/src/runtime/scheduler.go b/src/runtime/scheduler.go index 3af75017..22c7394d 100644 --- a/src/runtime/scheduler.go +++ b/src/runtime/scheduler.go @@ -1,4 +1,3 @@ - package runtime // This file implements the Go scheduler using coroutines. @@ -212,7 +211,7 @@ func scheduler(main taskInstance) { // Add tasks that are done sleeping to the end of the runqueue so they // will be executed soon. - if sleepQueue != nil && now - sleepQueueBaseTime >= uint64(taskPromise(sleepQueue).data) { + if sleepQueue != nil && now-sleepQueueBaseTime >= uint64(taskPromise(sleepQueue).data) { scheduleLog(" run <- sleep") t := sleepQueue promise := taskPromise(t) diff --git a/src/runtime/time.go b/src/runtime/time.go index 86475de7..3bdddaf3 100644 --- a/src/runtime/time.go +++ b/src/runtime/time.go @@ -1,4 +1,3 @@ - package runtime // TODO: use the time package for this. diff --git a/src/syscall/syscall.go b/src/syscall/syscall.go index 8571232c..d7ebaf3a 100644 --- a/src/syscall/syscall.go +++ b/src/syscall/syscall.go @@ -1,4 +1,3 @@ - package syscall // dummy diff --git a/src/unsafe/unsafe.go b/src/unsafe/unsafe.go index 0e7b8c8b..aec105ae 100644 --- a/src/unsafe/unsafe.go +++ b/src/unsafe/unsafe.go @@ -1,4 +1,3 @@ - package unsafe // dummy diff --git a/tgo.go b/tgo.go index 8a445b83..ff827f3a 100644 --- a/tgo.go +++ b/tgo.go @@ -1,4 +1,3 @@ - package main import ( @@ -127,14 +126,14 @@ func NewCompiler(pkgName, triple string, dumpSSA bool) (*Compiler, error) { func (c *Compiler) Parse(mainPath string, buildTags []string) error { tripleSplit := strings.Split(c.triple, "-") - config := loader.Config { + config := loader.Config{ TypeChecker: types.Config{ Sizes: &types.StdSizes{ int64(c.targetData.PointerSize()), int64(c.targetData.PrefTypeAlignment(c.i8ptrType)), }, }, - Build: &build.Context { + Build: &build.Context{ GOARCH: tripleSplit[0], GOOS: tripleSplit[2], GOROOT: ".", @@ -159,7 +158,7 @@ func (c *Compiler) Parse(mainPath string, buildTags []string) error { } } - c.program = ssautil.CreateProgram(lprogram, ssa.SanityCheckFunctions | ssa.BareInits) + c.program = ssautil.CreateProgram(lprogram, ssa.SanityCheckFunctions|ssa.BareInits) c.program.Build() c.mainPkg = c.program.ImportedPackage(mainPath) @@ -1219,7 +1218,7 @@ func (c *Compiler) parseExpr(frame *Frame, expr ssa.Value) (llvm.Value, error) { if !boundsCheck.IsNil() { constZero := llvm.ConstInt(c.intType, 0, false) isNegative := c.builder.CreateICmp(llvm.IntSLT, index, constZero, "") // index < 0 - isTooBig := c.builder.CreateICmp(llvm.IntSGE, index, buflen, "") // index >= len(value) + isTooBig := c.builder.CreateICmp(llvm.IntSGE, index, buflen, "") // index >= len(value) isOverflow := c.builder.CreateOr(isNegative, isTooBig, "") c.builder.CreateCall(boundsCheck, []llvm.Value{isOverflow}, "") } @@ -1382,7 +1381,7 @@ func (c *Compiler) parseBinOp(frame *Frame, binop *ssa.BinOp) (llvm.Value, error if typNamed, ok := typ.(*types.Named); ok { typ = typNamed.Underlying() } - signed := typ.(*types.Basic).Info() & types.IsUnsigned == 0 + signed := typ.(*types.Basic).Info()&types.IsUnsigned == 0 switch binop.Op { case token.ADD: // + return c.builder.CreateAdd(x, y, ""), nil @@ -1404,7 +1403,7 @@ func (c *Compiler) parseBinOp(frame *Frame, binop *ssa.BinOp) (llvm.Value, error } case token.AND: // & return c.builder.CreateAnd(x, y, ""), nil - case token.OR: // | + case token.OR: // | return c.builder.CreateOr(x, y, ""), nil case token.XOR: // ^ return c.builder.CreateXor(x, y, ""), nil @@ -1487,10 +1486,10 @@ func (c *Compiler) parseConst(expr *ssa.Const) (llvm.Value, error) { return llvm.Value{}, errors.New("todo: non-null constant pointer") } return llvm.ConstNull(c.i8ptrType), nil - } else if typ.Info() & types.IsUnsigned != 0 { + } else if typ.Info()&types.IsUnsigned != 0 { n, _ := constant.Uint64Val(expr.Value) return llvm.ConstInt(llvmType, n, false), nil - } else if typ.Info() & types.IsInteger != 0 { // signed + } else if typ.Info()&types.IsInteger != 0 { // signed n, _ := constant.Int64Val(expr.Value) return llvm.ConstInt(llvmType, uint64(n), true), nil } else { @@ -1532,13 +1531,13 @@ func (c *Compiler) parseConvert(frame *Frame, typeTo types.Type, x ssa.Value) (l return c.builder.CreateBitCast(value, llvmTypeTo, ""), nil } - if typeTo.Info() & types.IsInteger == 0 { // if not integer + if typeTo.Info()&types.IsInteger == 0 { // if not integer return llvm.Value{}, errors.New("todo: convert: extend non-integer type") } if sizeFrom > sizeTo { return c.builder.CreateTrunc(value, llvmTypeTo, ""), nil - } else if typeTo.Info() & types.IsUnsigned != 0 { // if unsigned + } else if typeTo.Info()&types.IsUnsigned != 0 { // if unsigned return c.builder.CreateZExt(value, llvmTypeTo, ""), nil } else { // if signed return c.builder.CreateSExt(value, llvmTypeTo, ""), nil @@ -1718,7 +1717,6 @@ func Compile(pkgName, runtimePath, outpath, target string, printIR, dumpSSA bool return nil } - func main() { outpath := flag.String("o", "", "output filename") printIR := flag.Bool("printir", false, "print LLVM IR") @@ -1734,7 +1732,7 @@ func main() { return } - os.Setenv("CC", "clang -target=" + *target) + os.Setenv("CC", "clang -target="+*target) err := Compile(flag.Args()[0], *runtime, *outpath, *target, *printIR, *dumpSSA) if err != nil {