Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/korlibs/ktcc
C Compiler that generates readable Kotlin and C# - Written in Kotlin + Small web-based Editor with autocompletion
https://github.com/korlibs/ktcc
ace-editor c c-compiler compiler docker-image kotlin kotlin-js kotlin-native
Last synced: 6 days ago
JSON representation
C Compiler that generates readable Kotlin and C# - Written in Kotlin + Small web-based Editor with autocompletion
- Host: GitHub
- URL: https://github.com/korlibs/ktcc
- Owner: korlibs
- License: mit
- Created: 2019-01-13T21:17:11.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2024-08-22T08:56:06.000Z (5 months ago)
- Last Synced: 2025-01-09T01:38:02.594Z (13 days ago)
- Topics: ace-editor, c, c-compiler, compiler, docker-image, kotlin, kotlin-js, kotlin-native
- Language: Kotlin
- Homepage: https://korlibs.github.io/ktcc/
- Size: 4.01 MB
- Stars: 71
- Watchers: 4
- Forks: 6
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
README
# ktcc
A C Compiler written in Multiplatform Kotlin (JVM, JS and Native).
[![Actions Status](https://github.com/soywiz/ktcc/workflows/Java%20CI/badge.svg)](https://github.com/soywiz/ktcc/actions)
## [Online live editor](https://korlibs.github.io/ktcc/) ([Video](https://youtu.be/r-yeEjJ0Ld0))
## [MASTER ONLINE LIVE DEMO](https://korlibs.github.io/ktcc/)
It aims to generate Kotlin and potentially other targets from ANSI C code.
Use cases:
* Compile C libraries into multiplatform Kotlin projects (that works on the JVM, JS and Native)
* Convert C libraries into logical Kotlin code that can be modifiedUsing:
* Provided as JVM library and CLI tool.
* Provided as native executable (linux, mac and windows).
* Provided as [docker image](https://cloud.docker.com/repository/docker/soywiz/ktcc/) with the compiler native executable. Just run latest uploaded version with: `docker run --rm "-v$PWD:/data" soywiz/ktcc $*`
* Provided as JavaScript library and [pure client-side online service](https://korlibs.github.io/ktcc/). It autocompletes and generates Kotlin code on the fly on your browser.CLI usage:
```kotlin
./gradlew fatJar
java -jar build/libs/ktcc-all.jar samples/simple.c # outputs the kotlin code
```Compile the c compiler with kotlin-native as a executable without dependencies:
```kotlin
./gradlew linkReleaseExecutableMacosArm64
./build/bin/macosArm64/releaseExecutable/ktcc.kexe samples/simple.c
```Cross-compile the compiler native executable for windows or linux in a mac machine (requires docker):
```kotlin
./gradlew_linux linkReleaseExecutableLinuxX64 # linux
./gradlew_win linkReleaseExecutableMingwX64 # windows
```Compile the docker image:
```kotlin
./build_docker_image.sh
```Example:
```bash
./ktcc_jvm samples/mp3dec.c --subtarget=jvm --runtime -o samples/mp3dec.kt
kotlinc samples/mp3dec.kt -include-runtime -d samples/mp3dec.jar
java -jar samples/mp3dec.jar samples/mp31.mp3 samples/mp32.mp3.out.pcm
```## Ideas
### Handling structs with inline classes
While keeping the code using structures sane.
```kotlin
fun alloca(size: Int): CPointer = CPointer((STACK_PTR - size).also { STACK_PTR -= size })inline fun stackFrame(callback: () -> T): T {
val oldPos = STACK_PTR
return try { callback() } finally { STACK_PTR = oldPos }
}inline class MyStruct2(val ptr: Int) {
companion object {
val SIZEOF = 4
val OFFSET_a = 0
}
var a: Int get() = lw(ptr + OFFSET_a); set(value) = run { sw(ptr + OFFSET_a, value) }
}inline class MyStruct(val ptr: Int) {
companion object {
val SIZEOF = 12
val OFFSET_a = 0
val OFFSET_b = 4
val OFFSET_c = 8
}
var a: Int get() = lw(ptr + OFFSET_a); set(value) = run { sw(ptr + OFFSET_a, value) }
var b: MyStruct2 get() = MyStruct2(lw(ptr + OFFSET_b)); set(value) = run { sw(ptr + OFFSET_b, value.ptr) } // Pointer to MyStruct2
val c: MyStruct2 get() = MyStruct2(ptr + OFFSET_c); set(value) = run { /* memcpy */ }
}/////////////
fun test() = stackFrame {
val ms = MyStruct(alloca(MyStruct.SIZEOF))
ms.b = MyStruct2(10)
ms.b.a = 10
}
```