Skip to content

Commit

Permalink
Intrinsic Type Casting (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
AjaniBilby authored Oct 6, 2024
1 parent 79dafc8 commit 3db919d
Show file tree
Hide file tree
Showing 32 changed files with 761 additions and 160 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@ jobs:
run: deno test

- name: Compiler behaviour tests
run: npm run test:reflect
run: deno task test:salient
20 changes: 16 additions & 4 deletions deno.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,25 @@
{
"tasks": {
"build": "npx run-s build:*",
"build:syntax": "npx bnf-compile ./source/bnf/",
"build:compiler": "deno compile --output salient.exe -A ./source/cli.ts",
"test": "deno test && deno task test:salient",
"test:deno": "deno test",
"test:salient": "deno run -A ./source/cli.ts test ./tests",
"cli": "deno run -A ./source/cli.ts",
"sa-test": "deno run -A ./source/cli.ts test",
"compile": "deno run -A ./source/cli.ts compile"
},
"compilerOptions": {
"allowJs": true,
"strict": true,
"strictNullChecks": true,
"lib": ["ESNext", "DOM"]
"lib": ["ESNext", "DOM", "deno.ns"]
},
"imports": {
"~/": "./source/"
"~/": "./source/",
"bnf-parser": "npm:bnf-parser@^4.1.0",
"npm-run-all": "npm:npm-run-all@^4.1.5",
"chalk": "npm:chalk@^5.3.0"
},
"lint": {
"include": ["source/"],
Expand All @@ -25,7 +38,6 @@
"exclude": ["source/bnf/"]
},
"lock": false,
"nodeModulesDir": true,
"test": {
"include": ["tests/**"]
}
Expand Down
26 changes: 2 additions & 24 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,10 @@
"files": [
"bin/*"
],
"scripts": {
"build": "npx run-s build:*",
"build:syntax": "npx bnf-compile ./source/bnf/",
"build:compiler": "deno compile --output salient.exe -A ./source/cli.ts",
"test": "npx run-s test:*",
"test:deno": "deno test",
"test:reflect": "npm run sa-test ./tests",
"cli": "deno run -A ./source/cli.ts",
"sa-test": "deno run -A ./source/cli.ts test",
"compile": "deno run -A ./source/cli.ts compile"
},
"bin": {
"salient": "bin/cli.js"
},
"preferGlobal": true,
"engineStrict": true,
"engines": {
"node": ">=18"
"deno": ">=2"
},
"repository": {
"type": "git",
Expand All @@ -35,13 +21,5 @@
"bugs": {
"url": "https://github.com/ajanibilby/salient/issues"
},
"homepage": "https://salient.moe",
"dependencies": {
"chalk": "^5.3.0"
},
"devDependencies": {
"bnf-parser": "^4.1.0",
"npm-run-all": "^4.1.5",
"typescript": "^5.2.2"
}
"homepage": "https://salient.moe"
}
3 changes: 2 additions & 1 deletion source/bnf/syntax.bnf
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ void ::= "void" ;
hexidecimal ::= "0"->"9" | "a" -> "f";

integer ::= ...integer_u ;
integer_u ::= ( digit_nz digit* ) | zero ;
digitish ::= %"_" | digit;
integer_u ::= ( digit_nz digitish* ) | zero ;
zero ::= "0" ;
float ::= ...( integer "." integer_u ( "e" integer )? ) ;

Expand Down
19 changes: 18 additions & 1 deletion source/bnf/syntax.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,23 @@ export declare function Parse_Integer (i: string, refMapping?: boolean): _Shared
isPartial: boolean
}

export type Term_Digitish = {
type: 'digitish',
start: number,
end: number,
count: number,
ref: _Shared.ReferenceRange,
value: [
(Term_Digit)
]
}
export declare function Parse_Digitish (i: string, refMapping?: boolean): _Shared.ParseError | {
root: _Shared.SyntaxNode & Term_Digitish,
reachBytes: number,
reach: null | _Shared.Reference,
isPartial: boolean
}

export type Term_Integer_u = {
type: 'integer_u',
start: number,
Expand All @@ -413,7 +430,7 @@ export type Term_Integer_u = {
ref: _Shared.ReferenceRange,
value: [
Term_Digit_nz,
{ type: '(...)*', value: Array<Term_Digit>, start: number, end: number, count: number, ref: _Shared.ReferenceRange }
{ type: '(...)*', value: Array<Term_Digitish>, start: number, end: number, count: number, ref: _Shared.ReferenceRange }
]
} | Term_Zero)
]
Expand Down
5 changes: 4 additions & 1 deletion source/bnf/syntax.js

Large diffs are not rendered by default.

4 changes: 1 addition & 3 deletions source/cli.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
/// <reference lib="deno.ns" />

import * as colors from "https://deno.land/[email protected]/fmt/colors.ts";

import { Compile } from "~/compile.ts";
import { Panic } from "~/compiler/helper.ts";
import { Test } from "~/test.ts";

if (Deno.args.includes("--version")) {
console.log("version: 0.0.0");
console.info("version: 0.0.0");
Deno.exit(0);
}

Expand Down
20 changes: 13 additions & 7 deletions source/compile.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
/// <reference lib="deno.ns" />

import { resolve, join, relative } from "https://deno.land/[email protected]/path/mod.ts";
import { resolve, join, relative, dirname } from "https://deno.land/[email protected]/path/mod.ts";
import { existsSync } from "https://deno.land/[email protected]/fs/mod.ts";
import * as colors from "https://deno.land/[email protected]/fmt/colors.ts";

Expand All @@ -9,6 +7,7 @@ import Package from "~/compiler/package.ts";
import Project from "~/compiler/project.ts";
import { DisplayTimers, TimerStart, TimerEnd } from "~/helper.ts";
import { Panic } from "~/compiler/helper.ts";
import { File } from "~/compiler/file.ts";


export async function Compile(entry: string, config: {
Expand All @@ -22,9 +21,16 @@ export async function Compile(entry: string, config: {
);

const project = new Project();
const mainPck = new Package(project, root);
const mainPck = new Package(project, dirname(root));

let mainFile: File;
try {
mainFile = mainPck.import(root);
} catch (e) {
console.error(e);
Deno.exit(1);
}

const mainFile = mainPck.import(root);
const mainFunc = mainFile.namespace["main"];
if (!(mainFunc instanceof Function)) Panic(
`Main namespace is not a function: ${colors.cyan(mainFunc.constructor.name)}`
Expand Down Expand Up @@ -58,8 +64,8 @@ export async function Compile(entry: string, config: {
}
if (config.time) TimerEnd("wasm2wat");

console.log(new TextDecoder().decode(stdout));
console.log(` out: "out.wasm" + "out.wat"\n`);
console.info(new TextDecoder().decode(stdout));
console.info(` out: "out.wasm" + "out.wat"\n`);

if (config.time) DisplayTimers();
}
1 change: 0 additions & 1 deletion source/compiler/codegen/expression/constant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,6 @@ function CompileFloat(ctx: Context, syntax: Syntax.Term_Float, expect?: Intrinsi




const ESCAPE_SYMBOLS = {
"0": "\0",
"f": "\f",
Expand Down
50 changes: 49 additions & 1 deletion source/compiler/codegen/expression/helper.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import type * as Syntax from "~/bnf/syntax.d.ts";
import { red } from "https://deno.land/[email protected]/fmt/colors.ts";

import * as WasmTypes from "~/wasm/type.ts";
import { AssertUnreachable, LatentOffset } from "~/helper.ts";
import { BasePointerType, LinearType } from "~/compiler/codegen/expression/type.ts";
import { ReferenceRange } from "~/bnf/shared.js";
import { IntrinsicType } from "~/compiler/intrinsic.ts";
import { IntrinsicType, f32, f64, i32, i64 } from "~/compiler/intrinsic.ts";
import { Instruction } from "~/wasm/index.ts";
import { SourceView } from "~/parser.ts";
import { Context } from "~/compiler/codegen/context.ts";
Expand Down Expand Up @@ -57,6 +58,53 @@ export function Load(ctx: Context, type: IntrinsicType, offset: number | LatentO
}


export function InlineClamp(ctx: Context, type: IntrinsicType, min: number | null, max: number | null) {
let scope;
switch (type.bitcode) {
case WasmTypes.Intrinsic.i32: scope = Instruction.i32; break;
case WasmTypes.Intrinsic.i64: scope = Instruction.i64; break;
case WasmTypes.Intrinsic.f32: scope = Instruction.f32; break;
case WasmTypes.Intrinsic.f64: scope = Instruction.f64; break;
default: throw "Assert failed";
}

const x = ctx.scope.register.allocate(type.bitcode);
if (min !== null) {
ctx.block.push(Instruction.local.tee(x.ref));
ctx.block.push(scope.const(min));

if ("lt" in scope) ctx.block.push(scope.lt()); // float
else {
if (type.signed) ctx.block.push(scope.lt_s());
else ctx.block.push(scope.lt_u());
}

ctx.block.push(Instruction.if(type.bitcode,
[ scope.const(min) ],
[ Instruction.local.get(x.ref) ],
));
}

if (max !== null) {
ctx.block.push(Instruction.local.tee(x.ref));
ctx.block.push(scope.const(max));

if ("gt" in scope) ctx.block.push(scope.gt()); // float
else {
if (type.signed) ctx.block.push(scope.gt_s());
else ctx.block.push(scope.gt_u());
}

ctx.block.push(Instruction.if(type.bitcode,
[ scope.const(max) ],
[ Instruction.local.get(x.ref) ],
));
}

x.free();
}


export function ResolveLinearType(ctx: Context, type: LinearType, ref: ReferenceRange, strict = true) {
if (strict) {
const errs = type.getCompositionErrors();
Expand Down
Loading

0 comments on commit 3db919d

Please sign in to comment.