Skip to content

Commit

Permalink
bytecblockPcOffset
Browse files Browse the repository at this point in the history
  • Loading branch information
joe-p committed Sep 20, 2024
1 parent b653539 commit 1ed5989
Show file tree
Hide file tree
Showing 8 changed files with 52,251 additions and 12,079 deletions.
227 changes: 179 additions & 48 deletions examples/lsig_with_app/artifacts/OptInLsig.src_map.json

Large diffs are not rendered by default.

13,550 changes: 11,173 additions & 2,377 deletions examples/reti/artifacts/StakingPool.arc56_draft.json

Large diffs are not rendered by default.

13,550 changes: 11,173 additions & 2,377 deletions examples/reti/artifacts/StakingPool.src_map.json

Large diffs are not rendered by default.

18,050 changes: 14,508 additions & 3,542 deletions examples/reti/artifacts/ValidatorRegistry.arc56_draft.json

Large diffs are not rendered by default.

18,050 changes: 14,508 additions & 3,542 deletions examples/reti/artifacts/ValidatorRegistry.src_map.json

Large diffs are not rendered by default.

73 changes: 21 additions & 52 deletions src/lib/compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -536,7 +536,7 @@ export default class Compiler {
sourceInfo: {
source: number;
teal: number;
disassembledTeal?: number;
bytecblockPcOffset?: number[];
pc?: number[];
errorMessage?: string;
}[] = [];
Expand Down Expand Up @@ -7499,7 +7499,11 @@ declare type AssetFreezeTxn = Required<AssetFreezeParams>;

if (tVar === undefined) return arg;

if (this.isDynamicType(tVar.type) || isNumeric(tVar.type)) {
if (isNumeric(tVar.type)) {
return 0;
}

if (this.isDynamicType(tVar.type)) {
if (program === 'lsig' || program === 'approval') {
console.warn(
`WARNING: Due to dynamic template variable type for ${tVar.name} (${typeInfoToABIString(
Expand All @@ -7510,10 +7514,6 @@ declare type AssetFreezeTxn = Required<AssetFreezeParams>;
this.hasDynamicTemplateVar = true;
}

if (isNumeric(tVar.type)) {
return 0;
}

return '0x';
}

Expand Down Expand Up @@ -7600,53 +7600,22 @@ declare type AssetFreezeTxn = Required<AssetFreezeParams>;
addrLine.teal += ` ${json.hash}`;
}

if (!this.hasDynamicTemplateVar) {
this.sourceInfo.forEach((sm) => {
// eslint-disable-next-line no-param-reassign
sm.pc = this.lineToPc[sm.teal - 1];
});

return json;
let lasteBytecblockPc = 0;
let bytecblockLine = 0;
if (this.hasDynamicTemplateVar) {
bytecblockLine = this.teal[program].findIndex((t) => t.teal.trim().startsWith('bytecblock'));
lasteBytecblockPc = this.lineToPc[bytecblockLine].at(-1)!;
}

// Now dissasemble the program to get a mapping of source -> dissasembled TEAL

const disassembleResponse = await fetch(`${this.algodServer}:${this.algodPort}/v2/teal/disassemble`, {
method: 'POST',
headers: {
'X-Algo-API-Token': this.algodToken,
},
body: Buffer.from(json.result, 'base64'),
});

const dissasembleJson = await disassembleResponse.json();

const recompileResponse = await fetch(`${this.algodServer}:${this.algodPort}/v2/teal/compile?sourcemap=true`, {
method: 'POST',
headers: {
'Content-Type': 'text/plain',
'X-Algo-API-Token': this.algodToken,
},
body: dissasembleJson.result,
});

const recompiledJson = await recompileResponse.json();

if (recompileResponse.status !== 200) {
throw new Error(`Error when compiling disassembled program: ${response.statusText}: ${recompiledJson.message}`);
}

const recompiledMapping = await getSourceMap(recompiledJson.sourcemap.mappings);

// Look at both recompiledMapping and mapping and find the mapping of source teal -> recompiled teal line
Object.keys(recompiledMapping.pcToLine).forEach((pcKey) => {
const pc = Number(pcKey);
const recompiledLine = recompiledMapping.pcToLine[pc];
const originalLine = mapping.pcToLine[pc];

const sourceInfo = this.sourceInfo.find((si) => si.teal === originalLine + 1);

if (sourceInfo) sourceInfo.disassembledTeal = recompiledLine;
this.sourceInfo.forEach((sm) => {
if (this.hasDynamicTemplateVar) {
if (sm.teal - 1 <= bytecblockLine) return;
console.debug(lasteBytecblockPc, this.lineToPc);
// eslint-disable-next-line no-param-reassign
sm.bytecblockPcOffset = this.lineToPc[sm.teal - 1].map((pc) => pc - lasteBytecblockPc);
return;
}
// eslint-disable-next-line no-param-reassign
sm.pc = this.lineToPc[sm.teal - 1];
});

return json;
Expand Down
Loading

0 comments on commit 1ed5989

Please sign in to comment.