ASDAlexander77 / TypeScriptCompiler

TypeScript Compiler (by LLVM)
MIT License
588 stars 28 forks source link

Update to LLVM 17 #66

Closed theoparis closed 11 months ago

theoparis commented 11 months ago

Unfortunately there seems to be a lot of changes needed - such as changing llvm::Optional to std::optional. The CMakeLists.txt does not let me disable the runtime nor will it let me use my system LLVM 17 on Debian because find_package(Clang fails (MLIR and LLVM are found though).

I also don't see a way to pass --target to tsc as it seems to only accept -march. Are you planning on implementing cross compilation support? It seems to support mtriple but not freestanding with -nostdlib.

ASDAlexander77 commented 11 months ago

I was thinking to do it in near future

ASDAlexander77 commented 11 months ago

It seems to support mtriple but not freestanding with -nostdlib.

can u show case scenario when u need to use -nostdlib?

theoparis commented 11 months ago

Alright thanks. I was trying to make a operating system kernel for fun with this compiler and this means I need to compile for bare metal (x86_64-unknown-none). I just realized that I can use tsc to emit llvm IR which can be passed to llc to convert it to an object, and then I can link it with clang. However, I also realized that there does not seem to be a way to convert the following C program to Typescript with this compiler because there is no pointer -> array dereferencing.

unsigned char *vga_buffer = (unsigned char *)0xb8000;
*vga_buffer++ = 'h';
*vga_buffer++ = 0xFFFFFF;

I tried this:

let x = <Uint8Array>0xb8000;
x[0] = 'h';
x[1] = 0xFFFFFF;

However it segfaults the compiler. I think there would need to be pointer operation functions 🤔 Additionally, The typescript compiler seems to require an async runtime in the emitted llvm IR, and malloc/free and I couldn't find a way to disable it.

Thanks for creating this amazing project by the way, it is something that I've wanted to make for a while.

ASDAlexander77 commented 11 months ago

I just realized that I can use tsc to emit llvm IR which can be passed to llc to convert it to an object, and then I can link it with clang.

you can generate "obj" files in tsc no need to extra steps .. and u can use it in cmake. have a look into \TypeScriptCompiler\docs\how\cmake_vulkan\

However, I also realized that there does not seem to be a way to convert the following C program to Typescript with this compiler because there is no pointer -> array dereferencing.

You can do it in "hacky" way: for example I have construction in the code such as:

type int = TypeOf<1>;
const a: Reference<int> = 10;
const v = LoadReference<int>(a);

but you can create your C/C++ helper library to use in TypeScript for example:

addr_access.c:

int get_int_by_addr(intptr_t addr)
{
  return *(int*)addr;
}

typescript_file.ts:

type intptr_t = TypeOf<4294967297>;
declare get_int_by_addr(intptr_t);

main()
{
  const v = get_int_by_addr(10);
}
ASDAlexander77 commented 11 months ago

Ideally I wanted to finish "JS Builtin" classes which would allow to work with arrays such as buffer = new Uint8Array(addr); it would allow to operate with normal classes without direct access to memory

ASDAlexander77 commented 11 months ago

The typescript compiler seems to require an async runtime in the emitted llvm IR

If you are not using async/await compiler will remove unused functions. so just do not use async/await

ASDAlexander77 commented 11 months ago

and malloc/free

you can use gc allocator but do not provide gc.lib and write your own implementation for allocator so u need to provide your funcation such as GC_alloc, GC_free etc

ASDAlexander77 commented 11 months ago

switched to LLVM 17.0.2