charly-lang / charly

🐈 The Charly Programming Language | Written by @KCreate
https://charly-lang.github.io/charly/
MIT License
199 stars 10 forks source link

Rewrite the type system #119

Open KCreate opened 7 years ago

KCreate commented 7 years ago

Currently all types are boxed inside a class called BaseType. This is super inefficient as it allocates a lot of memory for a single Float64, Bool, String etc.

At the beginning the justification of that was the ease of development, but now it's just a burden on performance.

The interpreter should use the native crystal types for calculations, values etc.

The only exceptions will be the TObject, TArray and the TFunction, as they need more information to work.

Here is the (unoptimized) LLVM-IR generated by the crystal compiler to create a single TNumeric value for charly:

define internal %"Charly::TNumeric"* @"*Charly::TNumeric::new<Float64>:Charly::TNumeric"(double %value) #0 {
alloca:
  %_ = alloca %"Charly::TNumeric"*
  br label %entry

entry:                                            ; preds = %alloca
  %malloccall = tail call i8* @malloc(i32 ptrtoint (%"Charly::TNumeric"* getelementptr (%"Charly::TNumeric", %"Charly::TNumeric"* null, i32 1) to i32))
  %0 = bitcast i8* %malloccall to %"Charly::TNumeric"*
  %1 = bitcast %"Charly::TNumeric"* %0 to i8*
  call void @llvm.memset.p0i8.i32(i8* %1, i8 0, i32 ptrtoint (%"Charly::TNumeric"* getelementptr (%"Charly::TNumeric", %"Charly::TNumeric"* null, i32 1) to i32), i32 4, i1 false)
  %2 = getelementptr inbounds %"Charly::TNumeric", %"Charly::TNumeric"* %0, i32 0, i32 0
  store i32 6, i32* %2
  store %"Charly::TNumeric"* %0, %"Charly::TNumeric"** %_
  %3 = load %"Charly::TNumeric"*, %"Charly::TNumeric"** %_
  %4 = call double @"*Charly::TNumeric#initialize<Float64>:Float64"(%"Charly::TNumeric"* %3, double %value)
  %5 = load %"Charly::TNumeric"*, %"Charly::TNumeric"** %_
  ret %"Charly::TNumeric"* %5
}