Skip to content

Commit

Permalink
feat: Initial draft of the new backend.
Browse files Browse the repository at this point in the history
+ Func syntax tree
+ Essential functionality Func formatter
+ A new `codegen` package that includes the updated backend
+ Hacks in the compilation pipeline that allow to use both backends
  which is needed for differential testing.
  • Loading branch information
byakuren-hijiri committed Jul 12, 2024
1 parent b119b7b commit 3e7c7b4
Show file tree
Hide file tree
Showing 12 changed files with 2,610 additions and 8 deletions.
26 changes: 26 additions & 0 deletions src/codegen/abi.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { AstExpression, SrcInfo } from "../grammar/ast";
import { CompilerContext } from "../context";
import { TypeRef } from "../types/types";
import { FuncAstExpr } from "../func/syntax";

/**
* A static map of functions defining Func expressions for Tact ABI functions and methods.
*/
export type AbiFunction = {
name: string;
resolve: (ctx: CompilerContext, args: TypeRef[], loc: SrcInfo) => TypeRef;
generate: (
args: TypeRef[],
resolved: AstExpression[],
loc: SrcInfo,
) => FuncAstExpr;
};

// TODO
export const MapFunctions: Map<string, AbiFunction> = new Map([]);

// TODO
export const StructFunctions: Map<string, AbiFunction> = new Map([]);

// TODO
export const GlobalFunctions: Map<string, AbiFunction> = new Map([]);
103 changes: 103 additions & 0 deletions src/codegen/contract.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
import { CompilerContext } from "../context";
import { getAllTypes } from "../types/resolveDescriptors";
import { TypeDescription } from "../types/types";
import { getSortedTypes } from "../storage/resolveAllocation";
import { FuncAstModule, FuncAstComment } from "../func/syntax";
import { FunctionGen } from "./function";

/**
* Encapsulates generation of Func contracts from the Tact contract.
*/
export class ContractGen {
private constructor(
private ctx: CompilerContext,
private contractName: string,
private abiName: string,
) {}

static fromTact(
ctx: CompilerContext,
contractName: string,
abiName: string,
): ContractGen {
return new ContractGen(ctx, contractName, abiName);
}

/**
* Adds stdlib definitions to the generated module.
*/
private addStdlib(m: FuncAstModule): void {

Check failure on line 29 in src/codegen/contract.ts

View workflow job for this annotation

GitHub Actions / test (20, ubuntu-latest)

'm' is defined but never used. Allowed unused args must match /^_/u

Check failure on line 29 in src/codegen/contract.ts

View workflow job for this annotation

GitHub Actions / test (20, windows-latest)

'm' is defined but never used. Allowed unused args must match /^_/u

Check failure on line 29 in src/codegen/contract.ts

View workflow job for this annotation

GitHub Actions / test (20, macos-latest)

'm' is defined but never used. Allowed unused args must match /^_/u
// TODO
}

private addSerializers(m: FuncAstModule): void {

Check failure on line 33 in src/codegen/contract.ts

View workflow job for this annotation

GitHub Actions / test (20, ubuntu-latest)

'm' is defined but never used. Allowed unused args must match /^_/u

Check failure on line 33 in src/codegen/contract.ts

View workflow job for this annotation

GitHub Actions / test (20, windows-latest)

'm' is defined but never used. Allowed unused args must match /^_/u

Check failure on line 33 in src/codegen/contract.ts

View workflow job for this annotation

GitHub Actions / test (20, macos-latest)

'm' is defined but never used. Allowed unused args must match /^_/u
const sortedTypes = getSortedTypes(this.ctx);
for (const t of sortedTypes) {

Check failure on line 35 in src/codegen/contract.ts

View workflow job for this annotation

GitHub Actions / test (20, ubuntu-latest)

't' is assigned a value but never used. Allowed unused vars must match /^_/u

Check failure on line 35 in src/codegen/contract.ts

View workflow job for this annotation

GitHub Actions / test (20, ubuntu-latest)

Empty block statement

Check failure on line 35 in src/codegen/contract.ts

View workflow job for this annotation

GitHub Actions / test (20, windows-latest)

't' is assigned a value but never used. Allowed unused vars must match /^_/u

Check failure on line 35 in src/codegen/contract.ts

View workflow job for this annotation

GitHub Actions / test (20, windows-latest)

Empty block statement

Check failure on line 35 in src/codegen/contract.ts

View workflow job for this annotation

GitHub Actions / test (20, macos-latest)

't' is assigned a value but never used. Allowed unused vars must match /^_/u

Check failure on line 35 in src/codegen/contract.ts

View workflow job for this annotation

GitHub Actions / test (20, macos-latest)

Empty block statement
}
}

private addAccessors(m: FuncAstModule): void {

Check failure on line 39 in src/codegen/contract.ts

View workflow job for this annotation

GitHub Actions / test (20, ubuntu-latest)

'm' is defined but never used. Allowed unused args must match /^_/u

Check failure on line 39 in src/codegen/contract.ts

View workflow job for this annotation

GitHub Actions / test (20, windows-latest)

'm' is defined but never used. Allowed unused args must match /^_/u

Check failure on line 39 in src/codegen/contract.ts

View workflow job for this annotation

GitHub Actions / test (20, macos-latest)

'm' is defined but never used. Allowed unused args must match /^_/u
// TODO
}

private addInitSerializer(m: FuncAstModule): void {

Check failure on line 43 in src/codegen/contract.ts

View workflow job for this annotation

GitHub Actions / test (20, ubuntu-latest)

'm' is defined but never used. Allowed unused args must match /^_/u

Check failure on line 43 in src/codegen/contract.ts

View workflow job for this annotation

GitHub Actions / test (20, windows-latest)

'm' is defined but never used. Allowed unused args must match /^_/u

Check failure on line 43 in src/codegen/contract.ts

View workflow job for this annotation

GitHub Actions / test (20, macos-latest)

'm' is defined but never used. Allowed unused args must match /^_/u
// TODO
}

private addStorageFunctions(m: FuncAstModule): void {

Check failure on line 47 in src/codegen/contract.ts

View workflow job for this annotation

GitHub Actions / test (20, ubuntu-latest)

'm' is defined but never used. Allowed unused args must match /^_/u

Check failure on line 47 in src/codegen/contract.ts

View workflow job for this annotation

GitHub Actions / test (20, windows-latest)

'm' is defined but never used. Allowed unused args must match /^_/u

Check failure on line 47 in src/codegen/contract.ts

View workflow job for this annotation

GitHub Actions / test (20, macos-latest)

'm' is defined but never used. Allowed unused args must match /^_/u
// TODO
}

private addStaticFunctions(m: FuncAstModule): void {

Check failure on line 51 in src/codegen/contract.ts

View workflow job for this annotation

GitHub Actions / test (20, ubuntu-latest)

'm' is defined but never used. Allowed unused args must match /^_/u

Check failure on line 51 in src/codegen/contract.ts

View workflow job for this annotation

GitHub Actions / test (20, windows-latest)

'm' is defined but never used. Allowed unused args must match /^_/u

Check failure on line 51 in src/codegen/contract.ts

View workflow job for this annotation

GitHub Actions / test (20, macos-latest)

'm' is defined but never used. Allowed unused args must match /^_/u
// TODO
}

private addExtensions(m: FuncAstModule): void {

Check failure on line 55 in src/codegen/contract.ts

View workflow job for this annotation

GitHub Actions / test (20, ubuntu-latest)

'm' is defined but never used. Allowed unused args must match /^_/u

Check failure on line 55 in src/codegen/contract.ts

View workflow job for this annotation

GitHub Actions / test (20, windows-latest)

'm' is defined but never used. Allowed unused args must match /^_/u

Check failure on line 55 in src/codegen/contract.ts

View workflow job for this annotation

GitHub Actions / test (20, macos-latest)

'm' is defined but never used. Allowed unused args must match /^_/u
// TODO
}

/**
* Adds functions defined within the Tact contract to the generated Func module.
* TODO: Why do we need function from *all* the contracts?
*/
private addContractFunctions(m: FuncAstModule, c: TypeDescription): void {
// TODO: Generate init
for (const tactFun of c.functions.values()) {
const funcFun = FunctionGen.fromTact(this.ctx, tactFun).generate();

Check failure on line 66 in src/codegen/contract.ts

View workflow job for this annotation

GitHub Actions / test (20, ubuntu-latest)

'funcFun' is assigned a value but never used. Allowed unused vars must match /^_/u

Check failure on line 66 in src/codegen/contract.ts

View workflow job for this annotation

GitHub Actions / test (20, windows-latest)

'funcFun' is assigned a value but never used. Allowed unused vars must match /^_/u

Check failure on line 66 in src/codegen/contract.ts

View workflow job for this annotation

GitHub Actions / test (20, macos-latest)

'funcFun' is assigned a value but never used. Allowed unused vars must match /^_/u
}
}

private addMain(m: FuncAstModule): void {
// TODO
}

/**
* Adds a comment entry to the module.
*/
private addComment(m: FuncAstModule, c: FuncAstComment): void {
m.entries.push(c);
}

public generate(): FuncAstModule {
const m: FuncAstModule = { kind: "module", entries: [] };

const allTypes = Object.values(getAllTypes(this.ctx));
const contracts = allTypes.filter((v) => v.kind === "contract");
const contract = contracts.find((v) => v.name === this.contractName);
if (contract === undefined) {
throw Error(`Contract "${this.contractName}" not found`);
}

this.addStdlib(m);
this.addSerializers(m);
this.addAccessors(m);
this.addInitSerializer(m);
this.addStorageFunctions(m);
this.addStaticFunctions(m);
this.addExtensions(m);
contracts.forEach((c) => this.addContractFunctions(m, c));
this.addMain(m);

return m;
}
}
Loading

0 comments on commit 3e7c7b4

Please sign in to comment.