Commit Graph

49 Commits

Author SHA1 Message Date
Ayke van Laethem fcd44c02cd compiler: fix const complex numbers
This led to an assertion failure with a debug build of LLVM, but
apparently worked with a release build of LLVM.
2018-10-24 23:24:11 +02:00
Ayke van Laethem cb0a148cd7 compiler: fix map optimization
Not all uses of a map are call instructions. Don't assume they are.
TODO: investigate these uses and see whether they might be eliminated?
2018-10-24 12:37:47 +02:00
Ayke van Laethem 6c6a43310a compiler: fix invalid incoming block in complex typeassert flow
A single *ssa.BasicBlock may be split in multiple LLVM basic blocks due
to typeassert instructions. This means the incoming block and outgoing
block are different. PHI nodes need to get the result from the outgoing
block, which was fixed before, but incoming branches need to branch to
the incoming block, not the outgoing block.

Branching to the outgoing block led to a LLVM verification error when
compiling the fmt package.

Originally found in (*fmt.pp).handleMethods.
2018-10-23 15:00:37 +02:00
Ayke van Laethem 96f74ec153 compiler: support 64-bit numbers in bounds check 2018-10-23 14:07:27 +02:00
Ayke van Laethem 17e8c850f6 compiler: fix invalid use of extractvalue on vector types 2018-10-23 14:05:55 +02:00
Ayke van Laethem e2f6aedd9d compiler: implement comparing structs directly 2018-10-23 13:27:18 +02:00
Ayke van Laethem 82be43f4e6 compiler: implement deferring of immediately-applied closures
This is a common operation:

    freevar := ...
    defer func() {
        println("I am deferred:", freevar)
    }()

The function is thus an immediately applied closure. Only this form is
currently supported, support for regular (fat) function pointers should
be trivial to add but is not currently implemented as it wasn't
necessary to get fmt to compile.
2018-10-22 14:06:51 +02:00
Ayke van Laethem 9b9b66a09d compiler: add complex manipulation
* builtins: real, imag, complex
* printing of complex numbers

No support for complex arithmetic yet.
2018-10-22 13:49:03 +02:00
Ayke van Laethem 0a06c6014d compiler: special slice bounds check for 64-bit numbers
It is allowed to index with an int64 even on a 32-bit platform, so we
have to handle that case. But make sure the normal case isn't penalized
by using 32-bit numbers when possible.
2018-10-20 18:28:00 +02:00
Ayke van Laethem 239504d9f4 compiler: implement recover()
Doesn't do anything useful yet as running deferred calls are not
executed during panic. Only implemented to get code to compile.
2018-10-20 18:00:12 +02:00
Ayke van Laethem 7c2a6169b0 compiler: support comma-ok in map lookup 2018-10-20 17:54:16 +02:00
Ayke van Laethem da89464a63 compiler: compare slice against nil 2018-10-20 17:22:51 +02:00
Ayke van Laethem 3f05490846 compiler: fix odd bounds check failure with impossible typeassert 2018-10-20 17:21:47 +02:00
Ayke van Laethem 77d6d6c417 compiler: allow structs in map keys 2018-10-20 17:21:13 +02:00
Ayke van Laethem c0c1ccb381 compiler, runtime: implement delete builtin 2018-10-20 16:18:55 +02:00
Ayke van Laethem 19f7927515 compiler: compare booleans
Implement == and != for booleans.
2018-10-20 15:47:59 +02:00
Ayke van Laethem 6a95b84cd8 compiler: support all operations on untyped strings
Operations on strings were not always also supported on untyped strings.
Make sure all of those operations are supported for both.
2018-10-20 15:47:02 +02:00
Ayke van Laethem 3babdfdc00 compiler: fix runtime.mainWrapper linkage and debug info 2018-10-19 17:39:41 +02:00
Ayke van Laethem 963ba16d7b compiler: add support for the append builtin 2018-10-19 14:40:19 +02:00
Ayke van Laethem 392bba8394 compiler: add support for parameters to inline assembly 2018-10-15 19:37:09 +02:00
Ayke van Laethem 52199f4a14 compiler: eliminate created but never used maps 2018-10-12 17:00:39 +02:00
Ayke van Laethem 25e73a5439 compiler: align and zero-initialize stack allocated values 2018-10-12 16:57:17 +02:00
Ayke van Laethem 2917347ff5 compiler: implement operations on some named types 2018-10-12 16:53:55 +02:00
Ayke van Laethem e1e3dbdce6 compiler: correctly generate global hashmaps of size > 8
Static map generation used the last bucket instead of the first bucket
in the chain, which caused lots of missing entries in hashmaps with
multiple buckets (size > 8).
2018-10-10 14:06:59 +02:00
Ayke van Laethem ec73bd6a26 compiler: optimize runtime.stringToBytes calls
This optimization makes sure the following pattern doesn't do a heap
allocation (assuming Write doesn't modify the slice):

    var w *machine.UART = ...
    w.Write([]byte("foo"))

As long as Write doesn't modify the slice and LLVM can detect this, a
call to runtime.stringToBytes with the necessary allocation + copy is
avoided.
2018-10-09 14:18:12 +02:00
Ayke van Laethem 4219652535 compiler: add basic heap-to-stack optimization 2018-10-09 14:14:52 +02:00
Ayke van Laethem 3289dd7134 compiler: use a fresh context for every compilation 2018-10-08 20:18:12 +02:00
Ayke van Laethem e8f211935e compiler: fix expanded structs in invoke calls 2018-10-07 13:19:38 +02:00
Ayke van Laethem 4957db89f4 compiler: fix interface calls for big underlying values
When the underlying value of an interface does not fit in a pointer, a
pointer to the value was correctly inserted in the heap. However, the
receiving method still assumed it got the underlying value instead of a
pointer to it leading to a crash.

This commit inserts wrapper functions for method calls on interfaces.

The bug wasn't obvious as on a 64-bit system, the underlying value was
almost always put directly in the interface. However, it led to a crash
on the AVR platform where pointer are (usually) just 16 bits making it
far more likely that underlying values cannot be directly stored in an
interface.
2018-10-07 02:06:48 +02:00
Ayke van Laethem 482c5633dd compiler: put debug information on package initializer functions
Make sure package initializers show up in backtraces, as they should. In
practice, it doesn't actually break backtraces as these functions are
usually inlined anyway, but it may help to debug an error in
initialization code.
2018-10-06 23:50:35 +02:00
Ayke van Laethem 5db43e8d04 compiler: move Optimize() function to a separate file
In the future, there will be more optimizations. Let's keep them in a
separate file for separation of concerns.
2018-10-06 19:57:41 +02:00
Ayke van Laethem 5d2ffa79e5 compiler: improve debug info to cover initialization 2018-10-03 19:24:29 +02:00
Ayke van Laethem f107a24b72 all: use LLVM library provided by the system 2018-09-30 15:10:04 +02:00
Ayke van Laethem b6db84e916 main: use GOPATH from the environment
Be more compatible with the Go toolchain by setting GOPATH in the same
way. This makes it possible to flash and run examples from the standard
GOPATH instead of only from the source tree.
2018-09-29 22:30:45 +02:00
Ayke van Laethem 8d170d3bd2 all: change special type __volatile to pragma //go:volatile
This is one step towards removing unnecessary special casts in most
cases. It is also part of removing as much magic as possible from the
compiler (the pragma is explicit, the special name is not).
2018-09-28 13:17:03 +02:00
Ayke van Laethem 13cb7d6503 avr: add interrupt support
Interrupts are supported using a special //go:interrupt pragma.
For example:

//go:interrupt INT0_vect
func handleINT0() {
    // do something here
}
2018-09-25 13:47:20 +02:00
Ayke van Laethem 174b6333f8 compiler: fix expanding zero-length structs 2018-09-25 13:45:04 +02:00
Ayke van Laethem ed227b8fd3 all: fix errors reported by go vet 2018-09-24 17:22:59 +02:00
Ayke van Laethem c9ae72a105 all: allow -O0 optimization level 2018-09-24 16:17:42 +02:00
Ayke van Laethem 6191d4e1ac compiler: rename .Parse() to .Compiler()
The fact it was called Parse() is more of a historical accident, as the
compiler started out using the Go AST directly instead of Go SSA.
2018-09-24 15:55:38 +02:00
Ayke van Laethem 1b229a8f8b compiler: support compiling individual .go files
For example:

    tinygo run ./src/examples/test/test.go
2018-09-24 15:46:30 +02:00
Ayke van Laethem a561e9a9ac ir: move adding packages from the compiler 2018-09-24 15:46:30 +02:00
Ayke van Laethem 9df04a2170 compiler: fix panic on import errors
I think this is a problem in the loader package - it doesn't seem to
return errors for missing packages.
2018-09-24 15:46:30 +02:00
Ayke van Laethem 8a468786df compiler: use config struct for options 2018-09-24 15:46:05 +02:00
Ayke van Laethem fd6dda5e4f main: run the compiler from any path 2018-09-24 12:25:33 +02:00
Ayke van Laethem 2938437efc compiler: make string data unnamed addresses
This ensures LLVM can merge identical strings, and thus reduces code
size in some cases.
2018-09-23 03:03:40 +02:00
Ayke van Laethem 7cea40bcb5 compiler: small cleanup in call handling code 2018-09-23 03:01:10 +02:00
Ayke van Laethem fdfa810060 compiler: expand small structs
Use fields of small structs (3 or less fields when flattened
recursively) directly as parameter values.

Advantages:
  * Code size is slightly reduced, both on unix and nrf.
  * AVR can finally deal with struct parameters - at least the small
    ones. examples/test now compiles. A real fix for struct parameters
    should go into upstream LLVM, but this is a nice win.

fixes #20
2018-09-23 02:39:05 +02:00
Ayke van Laethem b2cbfa78ca compiler: refactor compiler into separate package 2018-09-22 20:32:28 +02:00