aboutsummaryrefslogtreecommitdiffhomepage
path: root/cgo
AgeCommit message (Collapse)Author
2021-10-31all: drop support for LLVM 10Ayke van Laethem
2021-09-29cgo: implement rudimentary C array decayingAyke van Laethem
This is just a first step. It's not complete, but it gets some real world C code to parse. This signature, from the ESP-IDF: esp_err_t esp_wifi_get_mac(wifi_interface_t ifx, uint8_t mac[6]); Was previously converted to something like this (pseudocode): C.esp_err_t esp_wifi_get_mac(ifx C.wifi_interface_t, mac [6]uint8) But this is not correct. C array parameters will decay. The array is passed by reference instead of by value. Instead, this would be the correct signature: C.esp_err_t esp_wifi_get_mac(ifx C.wifi_interface_t, mac *uint8) So that it can be called like this (using CGo): var mac [6]byte errCode := C.esp_wifi_get_mac(C.ESP_IF_WIFI_AP, &mac[0]) This stores the result in the 6-element array mac.
2021-09-28builder, cgo: support function definitions in CGo headersAyke van Laethem
For example, the following did not work before but does work with this change: // int add(int a, int b) { // return a + b; // } import "C" func main() { println("add:", C.add(3, 5)) } Even better, the functions in the header are compiled together with the rest of the Go code and so they can be optimized together! Currently, inlining is not yet allowed but const-propagation across functions works. This should be improved in the future.
2021-09-28cgo: fix line/column reporting in syntax error messagesAyke van Laethem
2021-09-28build: normalize target triples to match ClangAyke van Laethem
This commit changes a target triple like "armv6m-none-eabi" to "armv6m-unknown-unknow-eabi". The reason is that while the former is correctly parsed in Clang (due to normalization), it wasn't parsed correctly in LLVM meaning that the environment wasn't set to EABI. This change normalizes all target triples and uses the EABI environment (-eabi in the triple) for Cortex-M targets. This change also drops the `--target=` flag in the target JSON files, the flag is now added implicitly in `(*compileopts.Config).CFlags()`. This removes some duplication in target JSON files. Unfortunately, this change also increases code size for Cortex-M targets. It looks like LLVM now emits calls like __aeabi_memmove instead of memmove, which pull in slightly more code (they basically just call the regular C functions) and the calls themself don't seem to be as efficient as they could be. Perhaps this is a LLVM bug that will be fixed in the future, as this is a very common occurrence.
2021-09-27all: fix staticcheck warningsAyke van Laethem
This is a loose collection of small fixes flagged by staticcheck: - dead code - regexp expressions not using backticks (`foobar` / "foobar") - redundant types of slice and map initializers - misc other fixes Not all of these seem very useful to me, but in particular dead code is nice to fix. I've fixed them all just so that if there are problems, they aren't hidden in the noise of less useful issues.
2021-09-09cgo: don't normalize CGo tests anymoreAyke van Laethem
Normalization was required because previously we supported Go 1.13 and Go 1.14 at the same time. Now we've dropped support for both so this normalization is not necessary anymore. CGo support remains the same. It's just the test outputs that aren't normalized anymore.
2021-05-21cgo: implement prefix parsingAyke van Laethem
This implements expressions such as "-5" and "-5 - 2", in other words, negative numbers.
2021-05-21cgo: parse binary operatorsAyke van Laethem
This follows the Pratt parser design.
2021-05-21cgo: create skeleton of a Pratt parserAyke van Laethem
This converts the existing const parser to the basics of a Pratt parser, following the book "Writing An Interpreter In Go" by Thorsten Ball. It doesn't really do anything interesting yet, it simply converts the existing code (with existing tests) to the new structure.
2021-04-06cgo: add support for CFLAGS in .c filesAyke van Laethem
This patch adds support for passing CFLAGS added in #cgo lines of the CGo preprocessing phase to the compiler when compiling C files inside packages. This is expected and convenient but didn't work before.
2021-03-21builder, compiler: compile and cache packages in parallelAyke van Laethem
This commit switches from the previous behavior of compiling the whole program at once, to compiling every package in parallel and linking the LLVM bitcode files together for further whole-program optimization. This is a small performance win, but it has several advantages in the future: - There are many more things that can be done per package in parallel, avoiding the bottleneck at the end of the compiler phase. This should speed up the compiler futher. - This change is a necessary step towards a non-LTO build mode for fast incremental builds that only rebuild the changed package, when compiler speed is more important than binary size. - This change refactors the compiler in such a way that it will be easier to inspect the IR for one package only. Inspecting this IR will be very helpful for compiler developers.
2021-03-09all: replace strings.Replace with strings.ReplaceAllAyke van Laethem
This was an addition to Go 1.13 and results in slightly easier to read code.
2021-03-04all: remove support for LLVM 9Ayke van Laethem
This LLVM version breaks CI and is now relatively rather old anyway, so remove support for it. This also reverts a workaround for LLVM 9, see a9568932b ("maixbit: workaround to avoid medium code model").
2021-02-19Add support for Go 1.16.Elliott Sales de Andrade
2021-02-11cgo: add support for variadic functionsAyke van Laethem
This doesn't yet add support for actually making use of variadic functions, but at least allows (unintended) variadic functions like the following to work: void foo();
2021-01-19main: use LLVM 11 by default when linking LLVM dynamicallyAyke van Laethem
This doesn't affect the release builds but it is helpful for TinyGo developers.
2020-10-13main: add initial support for (in-development) LLVM 11Ayke van Laethem
This can be useful to test improvements in LLVM master and to make it possible to support LLVM 11 for the most part already before the next release. That also allows catching LLVM bugs early to fix them upstream. Note that tests do not yet pass for this LLVM version, but the TinyGo compiler can be built with the binaries from apt.llvm.org (at the time of making this commit).
2020-06-08cgo: use scanner.Error in libclangAyke van Laethem
Previously it would return a `*scanner.Error`, which is not supported in the error printer of the main package. This can easily be fixed by making it a regular object (instead of a pointer).
2020-05-21cgo: Add LDFlags supportLucas Teske
2020-04-12cgo: normalize test resultsElliott Sales de Andrade
This makes the result consistent across Go versions, by running a regex on the CGo output that wraps all single-line functions in a consistent way. Originally written by Elliott Sales de Andrade and modified by Ayke van Laethem.
2020-04-09main: switch to LLVM 10Ayke van Laethem
This commit also adds a bit of version independence, in particular for external commands. It also adds the LLVM version to the `tinygo version` command, which might help while debugging.
2020-03-22all: include picolibc for bare metal targetsAyke van Laethem
This is necessary for better CGo support on bare metal. Existing libraries expect to be able to include parts of libc and expect to be able to link to those symbols. Because with this all targets have a working libc, it is now possible to add tests to check that a libc in fact works basically. Not all parts of picolibc are included, such as the math or stdio parts. These should be added later, when needed. This commit also avoids the need for the custom memcpy/memset/memcmp symbols that are sometimes emitted by LLVM. The C library will take care of that.
2020-03-17cgo: make -I and -L paths absoluteAyke van Laethem
This is very useful for (conditionally) adding extra include paths relative to the package path.
2020-01-03cgo: fix a bug in number tokenizationAyke van Laethem
2020-01-03cgo: add support for symbolsAyke van Laethem
2019-12-29Add initial FreeBSD supportDmitri Goutnik
2019-12-08cgo: don't run tests in parallelAyke van Laethem
Since LLVM 9, CGo sometimes randomly breaks with weird error messages on Windows. I'm not sure why this is the case, but it might be related to concurrency. Disable concurrency for now, and hope that will make the errors go away.
2019-11-25cgo: implement #cgo CFLAGSAyke van Laethem
This implementation is still very limited but provides a base to build upon. Limitations: * CGO_CFLAGS etc is not taken into account. * These CFLAGS are not used in C files compiled with the package. * Other flags (CPPFLAGS, LDFAGS, ...) are not yet implemented.
2019-11-25cgo: add tests for errorsAyke van Laethem
This commit adds tests for CGo preprocessing. There are various errors that can be reported while preprocessing, and they should integrate well with the compiler (including accurate source location tracking). Also allow CGo preprocessing to continue after Clang encountered an error, for a better view of what happened.
2019-11-16all: switch to LLVM 9Ayke van Laethem
2019-11-08cgo: add -update flag to testsAyke van Laethem
When this flag is set, the testdata/*.out.go files will be updated when they have changed. This is very convenient for updating these files after the expected output changes. Of course, the updated output must still be checked for validity.
2019-11-08cgo: add support for nested structs and unionsAyke van Laethem
2019-11-07cgo: refactor union supportAyke van Laethem
Instead of putting the magic in the AST, generate regular accessor methods. This avoids a number of special cases in the compiler, and avoids missing any of them. The resulting union accesses are somewhat clunkier to use, but the compiler implementation has far less coupling between the CGo implementation and the IR generator.
2019-11-06cgo: include all enums in the CGo Go ASTAyke van Laethem
Not all enums may be used as a type anywhere, which was previously the only way to include an enum in the AST. This commit makes sure all enums are included.
2019-11-06cgo: do type checking in CGo testingAyke van Laethem
Such type checking should hopefully catch more bugs. This commit also fixes some existing type errors.
2019-11-06cgo: avoid '"unsafe" imported but not used' errorAyke van Laethem
This can happen when not all CGo features are used.
2019-11-06cgo: rename reserved field names like `type`Ayke van Laethem
This commit renames reserved field names like `type` to `_type`, and in turn renames those fields as well (recursively). This avoids name clashes when a C struct contains a field named `type`, which is a reserved keyword in Go. For some details, see: https://golang.org/cmd/cgo/#hdr-Go_references_to_C
2019-11-05cgo: implement the constant parser as a real parserAyke van Laethem
Previously it was just a combination of heuristics to try to fit a constant in an *ast.BasicLit. For more complex expressions, this is not enough. This change also introduces proper syntax error with locations, if parsing a constant failed. For example, this will print a real error message with source location: #define FOO 5)
2019-11-05cgo: refactor constant expressionsAyke van Laethem
Put them in a separate file for separation of concerns (making them testable) and add some tests.
2019-11-03macos: use llvm@8 instead of just llvm in pathsAyke van Laethem
This should hopefully avoid needing to use `brew switch`.
2019-11-03cgo: add tests for most C typesAyke van Laethem
2019-11-03cgo: add bare-bones testAyke van Laethem
2019-11-03cgo: create new GenDecl for every symbolAyke van Laethem
Previously, a GenDecl was shared between many different consts/vars/types. However, it actually makes much more sense not to bundle them as that is also the case in C. This makes the printed output of the CGo AST much nicer, and works around a bug in Go 1.11.
2019-11-03cgo: improve diagnosticsAyke van Laethem
This commit improves diagnostics in a few ways: * All panics apart from panics with no (easy) recovery are converted to regular errors with source location. * Some errors were improved slightly to give more information. For example, show the libclang type kind as a string instead of a number. * Improve source location by respecting line directives in the C preprocessor. * Refactor to unify error handling between libclang diagnostics and failures to parse the libclang AST.
2019-06-03cgo: add support for anonymous structsAyke van Laethem
2019-06-03cgo: add support for bitfields using generated getters and settersAyke van Laethem
2019-05-17cgo: print better error messages for unknown typesAyke van Laethem
Types used in a program may not be implemented. Print a nice error message explaining the situation, instead of just prepending C. to the type spelling (and hoping the user knows what that undefined reference means).
2019-05-17cgo: add support for enum typesAyke van Laethem
Enum types are implemented as named types (with possible accompanying typedefs as type aliases). The constants inside the enums are treated as Go constants like in the Go toolchain.
2019-05-13Trivial typo fixesJustin Clift