Skip to content

add Unpack for unpacking integers. #80

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 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions src/implementation/c/code/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { MulAdd } from './mul-add';
import { Or } from './or';
import { Store } from './store';
import { Test } from './test';
import { Unpack } from './unpack';
import { Update } from './update';

export * from './base';
Expand Down
43 changes: 43 additions & 0 deletions src/implementation/c/code/unpack.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import * as frontend from 'llparse-frontend';
import * as assert from 'assert';
import { UNSIGNED_LIMITS, UNSIGNED_TYPES } from '../constants';
import { Compilation } from '../compilation';
import { Field } from './field';

export class Unpack extends Field<frontend.code.Unpack>{
// big endian will be rshift and little will be lshift
protected doBuild(ctx: Compilation, out: string[]): void {
const ty = ctx.getFieldType(this.ref.field);
assert(UNSIGNED_TYPES.has(ty), `Unexpected Unpack type "${ty}"`);
const targetTy = UNSIGNED_TYPES.get(ty)!;
const limit = UNSIGNED_LIMITS.get(ty)!;
const match = ctx.matchVar();

/* NOTE: (Vizonex) Feel free to correct me if I'm wrong here about this C code. */
if (!this.ref.bigEndian){
/* lshift */
out.push(`if (${this.field(ctx)} > 0){`);
out.push(` ${targetTy} __lshift_val = ${this.field(ctx)} << 8;`);
out.push(` if (__lshift_val > ${limit[1]} || __lshift_val < ${this.field(ctx)}){`);
out.push(" /* overflow */");
out.push(" return 1;");
out.push(" }");
out.push(` ${this.field(ctx)} = __lshift_val | ${match}`);
} else {
out.push(`if (${this.field(ctx)} > 0){`);
out.push(` ${targetTy} __rshift_val = ${this.field(ctx)} >> 8;`);
out.push(` if (__rshift_val > ${limit[1]} || __rshift_val < ${this.field(ctx)}){`);
out.push(" /* overflow */");
out.push(` return 1;`);
out.push(` }`);
out.push(` ${this.field(ctx)} = ${match} | __rshift_val`);
out.push(`}`);

}
/** if first value simply setting it in should be enough... */
out.push("} else {");
out.push(` ${this.field(ctx)} = ${match};`);
out.push("} ");
out.push("return 0;");
}
}