Skip to content

[PoC] Parse integer data #32

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 5 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,6 @@
"dependencies": {
"bitcode": "^1.2.0",
"debug": "^3.2.6",
"llparse-frontend": "^1.2.1"
"llparse-frontend": "git+https://github.com/arthurschreiber/llparse-frontend.git#arthur/binary-parsing"
}
}
2 changes: 2 additions & 0 deletions src/implementation/bitcode/node/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { Consume } from './consume';
import { Empty } from './empty';
import { Error as ErrorNode } from './error';
import { Invoke } from './invoke';
import { Int } from './int';
import { Pause } from './pause';
import { Sequence } from './sequence';
import { Single } from './single';
Expand All @@ -17,6 +18,7 @@ export default {
Consume,
Empty,
Error: class Error extends ErrorNode<frontend.node.Error> {},
Int,
Invoke,
Pause,
Sequence,
Expand Down
19 changes: 19 additions & 0 deletions src/implementation/bitcode/node/int.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import * as assert from 'assert';
import * as frontend from 'llparse-frontend';

import { Code } from '../code';
import { IRBasicBlock, IRValue } from '../compilation';
import { CONTAINER_KEY } from '../constants';
import { INodePosition, Node } from './base';

export class Int extends Node<frontend.node.Int> {
protected doBuild(bb: IRBasicBlock, pos: INodePosition): void {
const otherwise = this.ref.otherwise!;

if (!otherwise.noAdvance) {
bb = this.prologue(bb, pos);
}

this.tailTo(bb, otherwise, pos);
}
}
2 changes: 2 additions & 0 deletions src/implementation/c/node/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import * as frontend from 'llparse-frontend';
import { Consume } from './consume';
import { Empty } from './empty';
import { Error as ErrorNode } from './error';
import { Int } from './int';
import { Invoke } from './invoke';
import { Pause } from './pause';
import { Sequence } from './sequence';
Expand All @@ -17,6 +18,7 @@ export default {
Consume,
Empty,
Error: class Error extends ErrorNode<frontend.node.Error> {},
Int,
Invoke,
Pause,
Sequence,
Expand Down
10 changes: 10 additions & 0 deletions src/implementation/c/node/int.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import * as frontend from 'llparse-frontend';

import { Compilation } from '../compilation';
import { Node } from './base';

export class Int extends Node<frontend.node.Int> {
public doBuild(out: string[]): void {

}
}
2 changes: 2 additions & 0 deletions src/implementation/js/node/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import * as frontend from 'llparse-frontend';
import { Consume } from './consume';
import { Empty } from './empty';
import { Error as ErrorNode } from './error';
import { Int } from './int';
import { Invoke } from './invoke';
import { Pause } from './pause';
import { Sequence } from './sequence';
Expand All @@ -17,6 +18,7 @@ export default {
Consume,
Empty,
Error: class Error extends ErrorNode<frontend.node.Error> {},
Int,
Invoke,
Pause,
Sequence,
Expand Down
196 changes: 196 additions & 0 deletions src/implementation/js/node/int.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,196 @@
import * as frontend from 'llparse-frontend';

import { Compilation } from '../compilation';
import { Node } from './base';

export class Int extends Node<frontend.node.Int> {
public doBuild(out: string[]): void {
this.prologue(out);

switch (this.ref.bytes) {
case 1: {
if (this.ref.signed) {
this.readInt8(out);
} else {
this.readUInt8(out);
}
break;
}

case 2: {
if (this.ref.littleEndian) {
this.readUInt16LE(out);
} else {
this.readUInt16BE(out);
}
break;
}

case 3: {
if (this.ref.littleEndian) {
this.readUInt24LE(out);
} else {
this.readUInt24BE(out);
}
break;
}

case 4: {
if (this.ref.littleEndian) {
this.readUInt32LE(out);
} else {
this.readUInt32BE(out);
}
break;
}
}

this.tailTo(out, this.ref.otherwise!);
}

private readInt8(out: string[]) {
const ctx = this.compilation;
const index = ctx.stateField(this.ref.field);

out.push(`${index} = (${ctx.bufArg()}[${ctx.offArg()}] & 2 ** 7) * 0x1fffffe;`)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpick: Let's use 1 << 7 instead of 2 ** 7. Mixing binary and floating point operators looks fishy.

I'm not sure that this line does what you want it to do, but I understand that this is work in progress. We can discuss it later.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is based on how Node.js Buffer also implements reading integers: https://github.com/nodejs/node/blob/f5512ff61ecb668c2f49b7c05d3227ef7aa5e85f/lib/internal/buffer.js#L416

I thought being consistent with core would make sense here? 🤔 I'm not sure what the pros or cons of switching to 1 << 7 would be. 🤷‍♂

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we could also just inline the result of the multiplication / shifting by changing this to:

Suggested change
out.push(`${index} = (${ctx.bufArg()}[${ctx.offArg()}] & 2 ** 7) * 0x1fffffe;`)
out.push(`${index} = (${ctx.bufArg()}[${ctx.offArg()}] & ${2 ** 7}) * 0x1fffffe;`);

What do you think?

}

private readUInt8(out: string[]) {
const ctx = this.compilation;
const index = ctx.stateField(this.ref.field);

out.push(`${index} = ${ctx.bufArg()}[${ctx.offArg()}];`)
}

private readUInt16LE(out: string[]) {
const ctx = this.compilation;
const index = ctx.stateField(this.ref.field);

switch (this.ref.byteOffset) {
case 0: {
out.push(`${index} = ${ctx.bufArg()}[${ctx.offArg()}];`)
break;
}

case 1: {
out.push(`${index} += ${ctx.bufArg()}[${ctx.offArg()}] * 2 ** 8;`);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpick: same here, let's use << for binary operations.

break;
}
}
}

private readUInt24LE(out: string[]) {
const ctx = this.compilation;
const index = ctx.stateField(this.ref.field);

switch (this.ref.byteOffset) {
case 0: {
out.push(`${index} = ${ctx.bufArg()}[${ctx.offArg()}];`)
break;
}

case 1: {
out.push(`${index} += ${ctx.bufArg()}[${ctx.offArg()}] * 2 ** 8;`);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpick: ditto.

break;
}

case 2: {
out.push(`${index} += ${ctx.bufArg()}[${ctx.offArg()}] * 2 ** 16;`);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpick: ditto.

break;
}
}
}

private readUInt32LE(out: string[]) {
const ctx = this.compilation;
const index = ctx.stateField(this.ref.field);

switch (this.ref.byteOffset) {
case 0: {
out.push(`${index} = ${ctx.bufArg()}[${ctx.offArg()}];`)
break;
}

case 1: {
out.push(`${index} += ${ctx.bufArg()}[${ctx.offArg()}] * 2 ** 8;`);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpick: ditto.

break;
}

case 2: {
out.push(`${index} += ${ctx.bufArg()}[${ctx.offArg()}] * 2 ** 16;`);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpick: ditto.

break;
}

case 3: {
out.push(`${index} += ${ctx.bufArg()}[${ctx.offArg()}] * 2 ** 24;`);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpick: ditto.

break;
}
}
}

private readUInt16BE(out: string[]) {
const ctx = this.compilation;
const index = ctx.stateField(this.ref.field);

switch (this.ref.byteOffset) {
case 0: {
out.push(`${index} = ${ctx.bufArg()}[${ctx.offArg()}] * 2 ** 8;`);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpick: ditto.

break;
}

case 1: {
out.push(`${index} += ${ctx.bufArg()}[${ctx.offArg()}];`)
break;
}
}
}

private readUInt24BE(out: string[]) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds like it could be generalized a bit. There is no runtime savings from having separate methods for 16, 24, etc bits. These functions are executed only at compile time, right?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. My thinking was to keep these separated for readability - having all the logic in a single huge if/elseif/else statement made this really hard to understand. 🤷‍♂ Let's see how this looks like once all the other operations are in place too.

const ctx = this.compilation;
const index = ctx.stateField(this.ref.field);

switch (this.ref.byteOffset) {
case 0: {
out.push(`${index} = ${ctx.bufArg()}[${ctx.offArg()}] * 2 ** 16;`);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpick: ditto.

break;
}

case 1: {
out.push(`${index} += ${ctx.bufArg()}[${ctx.offArg()}] * 2 ** 8;`);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpick: ditto.

break;
}

case 2: {
out.push(`${index} += ${ctx.bufArg()}[${ctx.offArg()}];`)
break;
}
}
}

private readUInt32BE(out: string[]) {
const ctx = this.compilation;
const index = ctx.stateField(this.ref.field);

switch (this.ref.byteOffset) {
case 0: {
out.push(`${index} = ${ctx.bufArg()}[${ctx.offArg()}] * 2 ** 24;`);
break;
}

case 1: {
out.push(`${index} += ${ctx.bufArg()}[${ctx.offArg()}] * 2 ** 16;`);
break;
}

case 2: {
out.push(`${index} += ${ctx.bufArg()}[${ctx.offArg()}] * 2 ** 8;`);
break;
}

case 3: {
out.push(`${index} += ${ctx.bufArg()}[${ctx.offArg()}];`)
break;
}
}
}
}