Skip to content

Commit 444f14c

Browse files
added support for native variadic arguments and added size validation for classes (#117)
* adding variadic func call * implementted native varargs * added test * updated test * adding convertf * progress of adding convertf * added converf * using convertf * added validation for params count for non-varargs calls * added support for adding obj files * added public property to manage removal of unused symbols * added index support * progress of fixes * progress * progress of implementing fixes and size field * refactoring sizeof * fixes to compare * added size field to ensure integrity * fixes to class size field * build scripts update
1 parent 7ab4ad0 commit 444f14c

20 files changed

Lines changed: 345 additions & 215 deletions

tag.bat

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
git tag -a v0.0-pre-alpha50 -m "pre alpha v0.0-50"
1+
git tag -a v0.0-pre-alpha51 -m "pre alpha v0.0-51"
22
git push origin --tags

tag_del.bat

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
git push --delete origin v0.0-pre-alpha50
2-
git tag -d v0.0-pre-alpha50
1+
git push --delete origin v0.0-pre-alpha51
2+
git tag -d v0.0-pre-alpha51

tsc/include/TypeScript/DOM.h

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -208,12 +208,7 @@ class FunctionPrototypeDOM
208208

209209
bool isMultiArgs()
210210
{
211-
if (params.size() == 0)
212-
{
213-
return false;
214-
}
215-
216-
return params.back()->getIsMultiArgsParam();
211+
return params.size() > 0 && params.back()->getIsMultiArgsParam();
217212
}
218213

219214
const mlir_ts::FunctionType &getFuncType() const

tsc/include/TypeScript/Defines.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#define CONSTRUCTOR_TEMPVAR_NAME ".ctor"
2020
#define VTABLE_NAME ".vtbl"
2121
#define RTTI_NAME ".rtti"
22+
#define SIZE_NAME ".size"
2223
#define INSTANCEOF_NAME ".instanceOf"
2324
#define INSTANCEOF_PARAM_NAME "rttiParam"
2425
#define MAIN_ENTRY_NAME "main"

tsc/include/TypeScript/LowerToLLVM/CodeLogicHelper.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ class CodeLogicHelper
9494

9595
mlir::Value createIndexConstantOf(mlir::Type llvmIndexType, int64_t value)
9696
{
97-
return rewriter.create<LLVM::ConstantOp>(loc, llvmIndexType, rewriter.getIntegerAttr(llvmIndexType, value));
97+
return rewriter.create<LLVM::ConstantOp>(loc, llvmIndexType, rewriter.getIndexAttr(value));
9898
}
9999

100100
mlir::Value createStructIndexConstantOf(int32_t value)

tsc/include/TypeScript/LowerToLLVM/ConvertLogic.h

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,10 @@ class ConvertLogic
2929
CodeLogicHelper clh;
3030
Location loc;
3131

32-
protected:
33-
mlir::Type typeOfValueType;
34-
3532
public:
3633
ConvertLogic(Operation *op, PatternRewriter &rewriter, TypeConverterHelper &tch, Location loc, CompileOptions &compileOptions)
3734
: op(op), rewriter(rewriter), tch(tch), th(rewriter), ch(op, rewriter, &tch.typeConverter, compileOptions), clh(op, rewriter), loc(loc)
3835
{
39-
typeOfValueType = th.getI8PtrType();
4036
}
4137

4238
mlir::Value itoa(mlir::Value value)
@@ -92,17 +88,8 @@ class ConvertLogic
9288
{
9389
auto i8PtrTy = th.getI8PtrType();
9490

95-
#ifdef WIN32
96-
auto sprintfFuncOp = ch.getOrInsertFunction(
97-
"sprintf_s", th.getFunctionType(rewriter.getI32Type(), {th.getI8PtrType(), rewriter.getI32Type(), th.getI8PtrType()}, true));
98-
#else
99-
auto sprintfFuncOp = ch.getOrInsertFunction(
100-
"snprintf", th.getFunctionType(rewriter.getI32Type(), {th.getI8PtrType(), rewriter.getI32Type(), th.getI8PtrType()}, true));
101-
#endif
102-
103-
auto bufferSizeValue = clh.createI32ConstantOf(buffSize);
104-
// auto newStringValue = rewriter.create<LLVM::AllocaOp>(loc, i8PtrTy, bufferSizeValue, true);
105-
auto newStringValue = ch.MemoryAllocBitcast(i8PtrTy, bufferSizeValue, MemoryAllocSet::Atomic);
91+
auto llvmIndexType = tch.convertType(th.getIndexType());
92+
auto bufferSizeValue = clh.createIndexConstantOf(llvmIndexType, buffSize);
10693

10794
auto opHash = std::hash<std::string>{}(format);
10895

@@ -111,11 +98,8 @@ class ConvertLogic
11198

11299
auto formatSpecifierCst = ch.getOrCreateGlobalString(formatVarName.str(), format);
113100

114-
mlir::Value valueAsLLVMType = rewriter.create<mlir_ts::DialectCastOp>(loc, tch.convertType(value.getType()), value);
115-
116-
rewriter.create<LLVM::CallOp>(loc, sprintfFuncOp, ValueRange{newStringValue, bufferSizeValue, formatSpecifierCst, valueAsLLVMType});
117-
118-
return newStringValue;
101+
auto newVal = rewriter.create<mlir_ts::ConvertFOp>(loc, th.getStringType(), bufferSizeValue, formatSpecifierCst, ValueRange{value});
102+
return newVal;
119103
}
120104

121105
mlir::Value sprintfOfF64(mlir::Value value)

tsc/include/TypeScript/MLIRLogic/MLIRCodeLogic.h

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -219,15 +219,15 @@ class MLIRCustomMethods
219219
static bool isInternalFunctionNameBuiltin (StringRef functionName)
220220
{
221221
static std::map<std::string, bool> m {
222-
{"print", true}, {"assert", true}, {"parseInt", true}, {"parseFloat", true}, {"isNaN", true}, {"sizeof", true}, {GENERATOR_SWITCHSTATE, true},
222+
{"print", true}, {"convertf", true}, {"assert", true}, {"parseInt", true}, {"parseFloat", true}, {"isNaN", true}, {"sizeof", true}, {GENERATOR_SWITCHSTATE, true},
223223
{"LoadLibraryPermanently", true}, { "SearchForAddressOfSymbol", true }, { "LoadReference", true }, { "ReferenceOf", true }};
224224
return m[functionName.str()];
225225
}
226226

227227
static bool isInternalFunctionNameNoBuiltin (StringRef functionName)
228228
{
229229
static std::map<std::string, bool> m {
230-
{"print", true}, {"assert", true}, {"sizeof", true}, {GENERATOR_SWITCHSTATE, true},
230+
{"print", true}, {"convertf", true}, {"assert", true}, {"sizeof", true}, {GENERATOR_SWITCHSTATE, true},
231231
{"LoadLibraryPermanently", true}, { "SearchForAddressOfSymbol", true }, { "LoadReference", true }, { "ReferenceOf", true }};
232232
return m[functionName.str()];
233233
}
@@ -239,6 +239,11 @@ class MLIRCustomMethods
239239
// print - internal command;
240240
return mlirGenPrint(location, operands, castFn, genContext);
241241
}
242+
else if (functionName == "convertf")
243+
{
244+
// print - internal command;
245+
return mlirGenConvertF(location, operands, castFn, genContext);
246+
}
242247
else if (functionName == "assert")
243248
{
244249
// assert - internal command;
@@ -356,6 +361,39 @@ class MLIRCustomMethods
356361
return mlir::success();
357362
}
358363

364+
ValueOrLogicalResult mlirGenConvertF(const mlir::Location &location, ArrayRef<mlir::Value> operands, std::function<ValueOrLogicalResult(mlir::Location, mlir::Type, mlir::Value, const GenContext &)> castFn, const GenContext &genContext)
365+
{
366+
mlir::Value bufferSize;
367+
mlir::Value format;
368+
369+
if (operands.size() < 3) {
370+
return mlir::failure();
371+
}
372+
373+
bufferSize = operands[0];
374+
if (!bufferSize.getType().isa<mlir::IndexType>())
375+
{
376+
bufferSize = castFn(location, mlir::IndexType::get(builder.getContext()), bufferSize, genContext);
377+
}
378+
379+
auto stringType = mlir_ts::StringType::get(builder.getContext());
380+
format = operands[1];
381+
if (!format.getType().isa<mlir_ts::StringType>())
382+
{
383+
format = castFn(location, stringType, format, genContext);
384+
}
385+
386+
SmallVector<mlir::Value> vals;
387+
for (auto &oper : operands.take_back(operands.size() - 2))
388+
{
389+
vals.push_back(oper);
390+
}
391+
392+
auto convertFOp = builder.create<mlir_ts::ConvertFOp>(location, stringType, bufferSize, format, vals);
393+
394+
return V(convertFOp);
395+
}
396+
359397
mlir::LogicalResult mlirGenAssert(const mlir::Location &location, ArrayRef<mlir::Value> operands)
360398
{
361399
if (operands.size() == 0)

tsc/include/TypeScript/MLIRLogic/MLIRGenStore.h

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,21 +56,33 @@ enum class VariableScope
5656
Global
5757
};
5858

59+
enum class Select: int
60+
{
61+
NotSet = -1,
62+
Any = 0, //The linker may choose any COMDAT.
63+
ExactMatch = 1, //The data referenced by the COMDAT must be the same.
64+
Largest = 2, //The linker will choose the largest COMDAT.
65+
NoDeduplicate = 3, //No deduplication is performed.
66+
SameSize = 4, //The data referenced by the COMDAT must be the same size.
67+
};
68+
5969
struct VariableClass
6070
{
61-
VariableClass() : type{VariableType::Const}, isExport{false}, isImport{false}, isUsing{false}, isAppendingLinkage{false}
71+
VariableClass() : type{VariableType::Const}, isExport{false}, isImport{false}, isPublic{false}, isUsing{false}, isAppendingLinkage{false}, comdat{Select::NotSet}
6272
{
6373
}
6474

65-
VariableClass(VariableType type_) : type{type_}, isExport{false}, isImport{false}, isUsing{false}, isAppendingLinkage{false}
75+
VariableClass(VariableType type_) : type{type_}, isExport{false}, isImport{false}, isPublic{false}, isUsing{false}, isAppendingLinkage{false}, comdat{Select::NotSet}
6676
{
6777
}
6878

6979
VariableType type;
7080
bool isExport;
7181
bool isImport;
82+
bool isPublic;
7283
bool isUsing;
7384
bool isAppendingLinkage;
85+
Select comdat;
7486

7587
inline VariableClass& operator=(VariableType type_) { type = type_; return *this; }
7688

@@ -462,14 +474,16 @@ struct ClassInfo
462474
bool isAbstract;
463475
bool isExport;
464476
bool isImport;
477+
bool isPublic;
465478
bool isDynamicImport;
466479
bool hasRTTI;
467480
ProcessingStages processingAtEvaluation;
468481
ProcessingStages processing;
469482

470483
ClassInfo()
471484
: isDeclaration(false), hasNew(false), hasConstructor(false), hasInitializers(false), hasStaticConstructor(false),
472-
hasStaticInitializers(false), hasVirtualTable(false), isAbstract(false), isExport(false), isImport(false), isDynamicImport(false), hasRTTI(false),
485+
hasStaticInitializers(false), hasVirtualTable(false), isAbstract(false), isExport(false), isImport(false),
486+
isPublic(false), isDynamicImport(false), hasRTTI(false),
473487
processingAtEvaluation(ProcessingStages::NotSet), processing(ProcessingStages::NotSet)
474488
{
475489
}

tsc/include/TypeScript/TypeScriptOps.td

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,16 @@ def TypeScript_PrintOp : TypeScript_Op<"Print"> {
365365
let assemblyFormat = "`(` $inputs `)` attr-dict `:` type($inputs) ";
366366
}
367367

368+
def TypeScript_ConvertFOp : TypeScript_Op<"ConvertF"> {
369+
let summary = "format convert operation";
370+
let description = [{
371+
The "convertf" builtin operation to convert values into string with format.
372+
}];
373+
374+
let arguments = (ins Index:$bufferSize, TypeScript_String:$format, Variadic<AnyType>:$inputs);
375+
let results = (outs TypeScript_String:$result);
376+
}
377+
368378
def TypeScript_AssertOp : TypeScript_Op<"Assert"> {
369379
let summary = "Assert operation";
370380
let description = [{
@@ -580,9 +590,7 @@ def TypeScript_DeleteOp : TypeScript_Op<"Delete"> {
580590
let assemblyFormat = "`(` $reference `)` attr-dict `:` type($reference)";
581591
}
582592

583-
def TypeScript_CallOp : TypeScript_Op<"Call",
584-
[CallOpInterface,
585-
DeclareOpInterfaceMethods<SymbolUserOpInterface>]> {
593+
def TypeScript_CallOp : TypeScript_Op<"Call", [CallOpInterface, DeclareOpInterfaceMethods<SymbolUserOpInterface>]> {
586594
let summary = "call operation";
587595
let description = [{
588596
The `call` operation represents a direct call to a function that is within
@@ -596,7 +604,7 @@ def TypeScript_CallOp : TypeScript_Op<"Call",
596604
}];
597605

598606
let arguments = (ins FlatSymbolRefAttr:$callee, Variadic<AnyType>:$callOperands);
599-
let results = (outs Variadic<AnyType>);
607+
let results = (outs Variadic<AnyType>:$results);
600608

601609
let builders = [
602610
OpBuilder<(ins "FuncOp":$callee, CArg<"ValueRange", "{}">:$callOperands), [{
@@ -644,9 +652,6 @@ def TypeScript_CallOp : TypeScript_Op<"Call",
644652
def TypeScript_CallIndirectOp : TypeScript_Op<"CallIndirect", [
645653
CallOpInterface,
646654
DeclareOpInterfaceMethods<SymbolUserOpInterface>,
647-
TypesMatchWith<"callee input types match argument types",
648-
"callee", "callOperands",
649-
"($_self.isa<HybridFunctionType>() ? $_self.cast<HybridFunctionType>().getInputs() : $_self.cast<FunctionType>().getInputs())">,
650655
TypesMatchWith<"callee result types match result types",
651656
"callee", "results",
652657
"($_self.isa<HybridFunctionType>() ? $_self.cast<HybridFunctionType>().getResults() : $_self.cast<FunctionType>().getResults())">
@@ -699,7 +704,7 @@ def TypeScript_CallIndirectOp : TypeScript_Op<"CallIndirect", [
699704

700705
let hasCanonicalizer = 1;
701706

702-
let assemblyFormat = "$callee `(` $callOperands `)` attr-dict `:` type($callee)";
707+
let assemblyFormat = "$callee `[` type($callee) `]` `(` $callOperands `)` attr-dict `:` functional-type($callOperands, results)";
703708
}
704709

705710
def TypeScript_InvokeOp : TypeScript_Op<"Invoke", [

tsc/lib/TypeScript/LowerToAffineLoops.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1868,7 +1868,7 @@ void AddTsAffineLegalOps(ConversionTarget &target)
18681868
mlir_ts::ArithmeticUnaryOp, mlir_ts::AssertOp, mlir_ts::CastOp, mlir_ts::ConstantOp, mlir_ts::ElementRefOp,
18691869
mlir_ts::PointerOffsetRefOp, mlir_ts::FuncOp, mlir_ts::GlobalOp, mlir_ts::GlobalResultOp, mlir_ts::HasValueOp,
18701870
mlir_ts::ValueOp, mlir_ts::ValueOrDefaultOp, mlir_ts::NullOp, mlir_ts::ParseFloatOp, mlir_ts::ParseIntOp, mlir_ts::IsNaNOp,
1871-
mlir_ts::PrintOp, mlir_ts::SizeOfOp, mlir_ts::StoreOp, mlir_ts::SymbolRefOp, mlir_ts::LengthOfOp, mlir_ts::SetLengthOfOp,
1871+
mlir_ts::PrintOp, mlir_ts::ConvertFOp, mlir_ts::SizeOfOp, mlir_ts::StoreOp, mlir_ts::SymbolRefOp, mlir_ts::LengthOfOp, mlir_ts::SetLengthOfOp,
18721872
mlir_ts::StringLengthOp, mlir_ts::StringConcatOp, mlir_ts::StringCompareOp, mlir_ts::LoadOp, mlir_ts::NewOp,
18731873
mlir_ts::CreateTupleOp, mlir_ts::DeconstructTupleOp, mlir_ts::CreateArrayOp, mlir_ts::NewEmptyArrayOp,
18741874
mlir_ts::NewArrayOp, mlir_ts::DeleteOp, mlir_ts::PropertyRefOp, mlir_ts::InsertPropertyOp,

0 commit comments

Comments
 (0)