Giter Club home page Giter Club logo

Comments (5)

lung21 avatar lung21 commented on May 27, 2024 1
  1. Also, I see that 100 is handled as a floating-point number (double).
    Although Typescript considers all number as the number type, is there a way
    to know the intention whether it is an integer or a floating point number?

@taquangtrung

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures

At the abstract level, nodejs v8 engine is smart enough to interpret values of the Number type into double-precision numbers between -(2^53 − 1) and 2^53 − 1. MORE THAN THIS RANGE, the value of the Number type is to be represented as special symbols: -Infinity, Infinity, or NaN.

from tsll.

taquangtrung avatar taquangtrung commented on May 27, 2024

Thanks @lung21!

  1. Can you test with more generics of primitive types and print intput/output?

    let v1 = new AnotherClass<bool>(true);
    let v2 = new AnotherClass<string>("foo");
    ...
    
  2. Also, I see that 100 is handled as a floating-point number (double).

    Although Typescript considers all number as the number type, is there a way
    to know the intention whether it is an integer or a floating point number?

from tsll.

lung21 avatar lung21 commented on May 27, 2024

@taquangtrung Thank you...

To follow up, for the first question, we could successfully test generics of primitives and more nested generics as follows.

Input in Typescript

class AnotherClass<T> {
    private testPrivateMember: T;

    constructor(intro: T) {
        this.testPrivateMember = intro;
    }

    public testPrivateInPublic(): T {
        return this.testPrivateMember;
    }
}

class GenericClass<T> {
    private testPrivateMember: T;

    constructor(intro: T) {
        this.testPrivateMember = intro;
    }

    public testPrivateInPublic(): T {
        return this.testPrivateMember;
    }
}

let typicalGeneric = new GenericClass<number>(100);
let boolGeneric = new AnotherClass<string>('errc');
let nestedGeneric = new AnotherClass<AnotherClass<string>>(boolGeneric);

Output in LLVM IR

; ModuleID = 'genericType.ts'
source_filename = "genericType.ts"

%GenericClass_double = type { double }
%"AnotherClass_i8*" = type { i8* }
%"AnotherClass_%\22AnotherClass_i8*\22*" = type { %"AnotherClass_i8*"* }

@0 = private unnamed_addr constant [5 x i8] c"errc\00", align 1

define void @main(...) {
  %typicalGeneric = alloca %GenericClass_double, align 8
  call void (%GenericClass_double*, double, ...) @GenericClass_double_Constructor(%GenericClass_double* %typicalGeneric, double 1.000000e+02)
  %boolGeneric = alloca %"AnotherClass_i8*", align 8
  call void (%"AnotherClass_i8*"*, i8*, ...) @"AnotherClass_i8*_Constructor"(%"AnotherClass_i8*"* %boolGeneric, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @0, i32 0, i32 0))
  %nestedGeneric = alloca %"AnotherClass_%\22AnotherClass_i8*\22*", align 8
  call void (%"AnotherClass_%\22AnotherClass_i8*\22*"*, %"AnotherClass_i8*"*, ...) @"AnotherClass_%\22AnotherClass_i8*\22*_Constructor"(%"AnotherClass_%\22AnotherClass_i8*\22*"* %nestedGeneric, %"AnotherClass_i8*"* %boolGeneric)
  ret void
}

define void @GenericClass_double_Constructor(%GenericClass_double* %this, double %intro, ...) {
  %1 = alloca double, align 8
  store double %intro, double* %1, align 8
  %2 = getelementptr inbounds %GenericClass_double, %GenericClass_double* %this, i32 0, i32 0
  %3 = load double, double* %1, align 8
  store double %3, double* %2, align 8
  ret void
}

define double @GenericClass_double_testPrivateInPublic(%GenericClass_double* %this, ...) {
  %1 = getelementptr inbounds %GenericClass_double, %GenericClass_double* %this, i32 0, i32 0
  %2 = load double, double* %1, align 8
  ret double %2
}

define void @"AnotherClass_i8*_Constructor"(%"AnotherClass_i8*"* %this, i8* %intro, ...) {
  %1 = getelementptr inbounds %"AnotherClass_i8*", %"AnotherClass_i8*"* %this, i32 0, i32 0
  store i8* %intro, i8** %1, align 8
  ret void
}

define i8** @"AnotherClass_i8*_testPrivateInPublic"(%"AnotherClass_i8*"* %this, ...) {
  %1 = getelementptr inbounds %"AnotherClass_i8*", %"AnotherClass_i8*"* %this, i32 0, i32 0
  ret i8** %1
}

define void @"AnotherClass_%\22AnotherClass_i8*\22*_Constructor"(%"AnotherClass_%\22AnotherClass_i8*\22*"* %this, %"AnotherClass_i8*"* %intro, ...) {
  %1 = getelementptr inbounds %"AnotherClass_%\22AnotherClass_i8*\22*", %"AnotherClass_%\22AnotherClass_i8*\22*"* %this, i32 0, i32 0
  store %"AnotherClass_i8*"* %intro, %"AnotherClass_i8*"** %1, align 8
  ret void
}

define %"AnotherClass_i8*"** @"AnotherClass_%\22AnotherClass_i8*\22*_testPrivateInPublic"(%"AnotherClass_%\22AnotherClass_i8*\22*"* %this, ...) {
  %1 = getelementptr inbounds %"AnotherClass_%\22AnotherClass_i8*\22*", %"AnotherClass_%\22AnotherClass_i8*\22*"* %this, i32 0, i32 0
  ret %"AnotherClass_i8*"** %1
}

For the second question, although we have realized that Nodejs v8 engine may store number type in double or int32, we should investigate further and modify our typescriptllvm to allocate proper sizes for the number type in LLVM IR.

from tsll.

taquangtrung avatar taquangtrung commented on May 27, 2024

Thanks Lung! The generated code seems correct.

I notice that the parameters still keep names like %intro, %this. Maybe you want to rename it to LLVM SSA format similar to Clang to avoid potential naming conflict in the future?

from tsll.

lung21 avatar lung21 commented on May 27, 2024

Implemented, under test

from tsll.

Related Issues (12)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.