Skip to content
This repository has been archived by the owner on Sep 5, 2023. It is now read-only.

Imports path structure #968

Merged
merged 19 commits into from
Mar 8, 2023
Merged
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
2 changes: 1 addition & 1 deletion src/ast/ast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ export class AST {

registerImport(
node: ASTNode,
location: string,
location: string[],
name: string,
inputs: ParameterInfo[],
outputs: ParameterInfo[],
Expand Down
4 changes: 2 additions & 2 deletions src/ast/cairoNodes/cairoImportFunctionDefinition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ import { CairoFunctionDefinition, FunctionStubKind } from './cairoFunctionDefini
import { Implicits } from '../../utils/implicits';

export class CairoImportFunctionDefinition extends CairoFunctionDefinition {
path: string;
path: string[];
constructor(
id: number,
src: string,
scope: number,
name: string,
path: string,
path: string[],
implicits: Set<Implicits>,
parameters: ParameterList,
returnParameters: ParameterList,
Expand Down
42 changes: 23 additions & 19 deletions src/cairoUtilFuncGen/abi/abiDecode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,16 @@ import { uint256 } from '../../warplib/utils';
import { add, delegateBasedOnType, mul, StringIndexedFuncGenWithAuxiliar } from '../base';
import { MemoryWriteGen } from '../memory/memoryWrite';
import { removeSizeInfo } from './base';
import {
BYTE_ARRAY_TO_FELT_VALUE,
DYNAMIC_ARRAYS_UTIL,
FELT_TO_UINT256,
NARROW_SAFE,
UINT256,
WM_INDEX_DYN,
WM_NEW,
WM_ALLOC,
} from '../../utils/importPaths';

const IMPLICITS =
'{bitwise_ptr : BitwiseBuiltin*, range_check_ptr : felt, warp_memory : DictAccess*}';
Expand Down Expand Up @@ -210,9 +220,7 @@ export class AbiDecode extends StringIndexedFuncGenWithAuxiliar {
let initInstructions: string[] = [];
let typeIndex = 'mem_index';
if (isDynamicallySized(type, this.ast.inference)) {
importedFuncs.push(
this.requireImport('warplib.dynamic_arrays_util', 'byte_array_to_felt_value'),
);
importedFuncs.push(this.requireImport(...BYTE_ARRAY_TO_FELT_VALUE));
initInstructions = [
`let (param_offset) = byte_array_to_felt_value(mem_index, mem_index + 32, mem_ptr, 0);`,
`let mem_offset = ${calcOffset('mem_index', 'param_offset', relativeIndexVar)};`,
Expand Down Expand Up @@ -249,7 +257,7 @@ export class AbiDecode extends StringIndexedFuncGenWithAuxiliar {
`);`,
];
// Other relevant imports get added when the function is generated
importedFuncs.push(this.requireImport('warplib.memory', 'wm_new'));
importedFuncs.push(this.requireImport(...WM_NEW));
} else if (type instanceof ArrayType) {
// Handling static arrays
assert(type.size !== undefined);
Expand All @@ -266,7 +274,7 @@ export class AbiDecode extends StringIndexedFuncGenWithAuxiliar {
` ${decodeResult}`,
`);`,
];
importedFuncs.push(this.requireImport('warplib.memory', 'wm_alloc'));
importedFuncs.push(this.requireImport(...WM_ALLOC));
} else if (type instanceof UserDefinedType && type.definition instanceof StructDefinition) {
const maxSize = CairoType.fromSol(
type,
Expand All @@ -281,7 +289,7 @@ export class AbiDecode extends StringIndexedFuncGenWithAuxiliar {
` ${decodeResult}`,
`);`,
];
importedFuncs.push(this.requireImport('warplib.memory', 'wm_alloc'));
importedFuncs.push(this.requireImport(...WM_ALLOC));
} else {
throw new TranspileFailedError(
`Unexpected reference type to generate decoding code: ${printTypeNode(type)}`,
Expand Down Expand Up @@ -309,7 +317,7 @@ export class AbiDecode extends StringIndexedFuncGenWithAuxiliar {

if (byteSize === 32) {
args.push('0');
importedFuncs.push(this.requireImport('starkware.cairo.common.uint256', 'Uint256'));
importedFuncs.push(this.requireImport(...UINT256));
}
const decodeType = byteSize === 32 ? 'Uint256' : 'felt';

Expand Down Expand Up @@ -369,11 +377,7 @@ export class AbiDecode extends StringIndexedFuncGenWithAuxiliar {
const funcInfo = {
name,
code,
functionsCalled: [
this.requireImport('warplib.maths.utils', 'felt_to_uint256'),
...functionsCalled,
writeToMemFunc,
],
functionsCalled: [this.requireImport(...FELT_TO_UINT256), ...functionsCalled, writeToMemFunc],
};

const generatedFunc = this.createAuxiliarGeneratedFunction(funcInfo);
Expand Down Expand Up @@ -431,10 +435,10 @@ export class AbiDecode extends StringIndexedFuncGenWithAuxiliar {
].join('\n');

const importedFuncs = [
this.requireImport('starkware.cairo.common.uint256', 'Uint256'),
this.requireImport('warplib.memory', 'wm_index_dyn'),
this.requireImport('warplib.maths.utils', 'felt_to_uint256'),
this.requireImport('warplib.maths.utils', 'narrow_safe'),
this.requireImport(...UINT256),
this.requireImport(...WM_INDEX_DYN),
this.requireImport(...FELT_TO_UINT256),
this.requireImport(...NARROW_SAFE),
];

const funcInfo = {
Expand Down Expand Up @@ -502,7 +506,7 @@ export class AbiDecode extends StringIndexedFuncGenWithAuxiliar {
`}`,
].join('\n');

const importedFuncs = [this.requireImport('warplib.maths.utils', 'felt_to_uint256')];
const importedFuncs = [this.requireImport(...FELT_TO_UINT256)];
const genFuncInfo = {
name,
code,
Expand All @@ -516,13 +520,13 @@ export class AbiDecode extends StringIndexedFuncGenWithAuxiliar {

private createStringBytesDecoding(): CairoFunctionDefinition {
const funcName = 'memory_dyn_array_copy';
const importedFunc = this.requireImport('warplib.dynamic_arrays_util', funcName);
const importedFunc = this.requireImport(DYNAMIC_ARRAYS_UTIL, funcName);
return importedFunc;
}

private createValueTypeDecoding(byteSize: number | bigint): CairoFunctionDefinition {
const funcName = byteSize === 32 ? 'byte_array_to_uint256_value' : 'byte_array_to_felt_value';
const importedFunc = this.requireImport('warplib.dynamic_arrays_util', funcName);
const importedFunc = this.requireImport(DYNAMIC_ARRAYS_UTIL, funcName);
return importedFunc;
}
}
Expand Down
45 changes: 26 additions & 19 deletions src/cairoUtilFuncGen/abi/abiEncode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,17 @@ import { CairoFunctionDefinition } from '../../export';
import { printTypeNode } from '../../utils/astPrinter';
import { CairoType, MemoryLocation, TypeConversionContext } from '../../utils/cairoTypeSystem';
import { TranspileFailedError } from '../../utils/errors';
import {
ALLOC,
DYNAMIC_ARRAYS_UTIL,
FELT_ARRAY_TO_WARP_MEMORY_ARRAY,
FELT_TO_UINT256,
NARROW_SAFE,
UINT256,
WM_DYN_ARRAY_LENGTH,
WM_INDEX_DYN,
WM_NEW,
} from '../../utils/importPaths';
import {
getByteSize,
getElementType,
Expand Down Expand Up @@ -90,11 +101,11 @@ export class AbiEncode extends AbiBase {
].join('\n');

const importedFuncs = [
this.requireImport('starkware.cairo.common.alloc', 'alloc'),
this.requireImport('starkware.cairo.common.uint256', 'Uint256'),
this.requireImport('warplib.maths.utils', 'felt_to_uint256'),
this.requireImport('warplib.memory', 'wm_new'),
this.requireImport('warplib.dynamic_arrays_util', 'felt_array_to_warp_memory_array'),
this.requireImport(...ALLOC),
this.requireImport(...UINT256),
this.requireImport(...FELT_TO_UINT256),
this.requireImport(...WM_NEW),
this.requireImport(...FELT_ARRAY_TO_WARP_MEMORY_ARRAY),
];

const funcInfo = {
Expand Down Expand Up @@ -194,7 +205,7 @@ export class AbiEncode extends AbiBase {
// packed size of addresses is 32 bytes, but they are treated as felts,
// so they should be converted to Uint256 accordingly
if (size < 32 || isAddressType(type)) {
funcsCalled.push(this.requireImport(`warplib.maths.utils`, 'felt_to_uint256'));
funcsCalled.push(this.requireImport(...FELT_TO_UINT256));
instructions.push(`let (${varToEncode}256) = felt_to_uint256(${varToEncode});`);
varToEncode = `${varToEncode}256`;
}
Expand Down Expand Up @@ -260,9 +271,9 @@ export class AbiEncode extends AbiBase {
].join('\n');

const importedFuncs = [
this.requireImport('warplib.memory', 'wm_dyn_array_length'),
this.requireImport('warplib.maths.utils', 'felt_to_uint256'),
this.requireImport('warplib.maths.utils', 'narrow_safe'),
this.requireImport(...WM_DYN_ARRAY_LENGTH),
this.requireImport(...FELT_TO_UINT256),
this.requireImport(...NARROW_SAFE),
];

const genFuncInfo = {
Expand Down Expand Up @@ -316,8 +327,8 @@ export class AbiEncode extends AbiBase {
].join('\n');

const importedFuncs = [
this.requireImport('warplib.memory', 'wm_index_dyn'),
this.requireImport('warplib.maths.utils', 'felt_to_uint256'),
this.requireImport(...WM_INDEX_DYN),
this.requireImport(...FELT_TO_UINT256),
];

const genFuncInfo = {
Expand Down Expand Up @@ -376,7 +387,7 @@ export class AbiEncode extends AbiBase {
`}`,
].join('\n');

const importedFunc = this.requireImport('warplib.maths.utils', 'felt_to_uint256');
const importedFunc = this.requireImport(...FELT_TO_UINT256);

const genFuncInfo = {
name,
Expand Down Expand Up @@ -497,11 +508,7 @@ export class AbiEncode extends AbiBase {
const genFuncInfo = {
name,
code,
functionsCalled: [
this.requireImport('warplib.maths.utils', 'felt_to_uint256'),
inlineEncoding,
valueEncoding,
],
functionsCalled: [this.requireImport(...FELT_TO_UINT256), inlineEncoding, valueEncoding],
};
const auxFunc = this.createAuxiliarGeneratedFunction(genFuncInfo);

Expand Down Expand Up @@ -568,12 +575,12 @@ export class AbiEncode extends AbiBase {

private createStringOrBytesHeadEncoding(): CairoFunctionDefinition {
const funcName = 'bytes_to_felt_dynamic_array';
return this.requireImport('warplib.dynamic_arrays_util', funcName);
return this.requireImport(DYNAMIC_ARRAYS_UTIL, funcName);
}

private createValueTypeHeadEncoding(): CairoFunctionDefinition {
const funcName = 'fixed_bytes256_to_felt_dynamic_array';
return this.requireImport('warplib.dynamic_arrays_util', funcName);
return this.requireImport(DYNAMIC_ARRAYS_UTIL, funcName);
}

protected readMemory(type: TypeNode, arg: string): [string, CairoFunctionDefinition] {
Expand Down
39 changes: 22 additions & 17 deletions src/cairoUtilFuncGen/abi/abiEncodePacked.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,18 @@ import { uint256 } from '../../warplib/utils';
import { delegateBasedOnType, mul } from '../base';
import { MemoryReadGen } from '../memory/memoryRead';
import { AbiBase } from './base';
import {
ALLOC,
DYNAMIC_ARRAYS_UTIL,
FELT_ARRAY_TO_WARP_MEMORY_ARRAY,
FELT_TO_UINT256,
FIXED_BYTES256_TO_FELT_DYNAMIC_ARRAY,
NARROW_SAFE,
UINT256,
WM_DYN_ARRAY_LENGTH,
WM_INDEX_DYN,
WM_NEW,
} from '../../utils/importPaths';

const IMPLICITS =
'{bitwise_ptr : BitwiseBuiltin*, range_check_ptr : felt, warp_memory : DictAccess*}';
Expand Down Expand Up @@ -76,11 +88,11 @@ export class AbiEncodePacked extends AbiBase {
].join('\n');

const importedFuncs = [
this.requireImport('starkware.cairo.common.uint256', 'Uint256'),
this.requireImport('starkware.cairo.common.alloc', 'alloc'),
this.requireImport('warplib.maths.utils', 'felt_to_uint256'),
this.requireImport('warplib.memory', 'wm_new'),
this.requireImport('warplib.dynamic_arrays_util', 'felt_array_to_warp_memory_array'),
this.requireImport(...UINT256),
this.requireImport(...ALLOC),
this.requireImport(...FELT_TO_UINT256),
this.requireImport(...WM_NEW),
this.requireImport(...FELT_ARRAY_TO_WARP_MEMORY_ARRAY),
];

return {
Expand Down Expand Up @@ -120,8 +132,8 @@ export class AbiEncodePacked extends AbiBase {
`let ${newIndexVar} = bytes_index + 32;`,
].join('\n'),
[
this.requireImport('warplib.maths.utils', 'felt_to_uint256'),
this.requireImport('warplib.dynamic_arrays_util', 'fixed_bytes256_to_felt_dynamic_array'),
this.requireImport(...FELT_TO_UINT256),
this.requireImport(...FIXED_BYTES256_TO_FELT_DYNAMIC_ARRAY),
],
];
}
Expand All @@ -135,11 +147,7 @@ export class AbiEncodePacked extends AbiBase {
`let (length) = narrow_safe(length256);`,
`let (${newIndexVar}) = ${func.name}(bytes_index, bytes_array, 0, length, ${varToEncode});`,
].join('\n'),
[
this.requireImport('warplib.memory', 'wm_dyn_array_length'),
this.requireImport('warplib.maths.utils', 'narrow_safe'),
func,
],
[this.requireImport(...WM_DYN_ARRAY_LENGTH), this.requireImport(...NARROW_SAFE), func],
];
}

Expand Down Expand Up @@ -225,10 +233,7 @@ export class AbiEncodePacked extends AbiBase {
].join('\n');

const importedFuncs = isDynamicArray(type)
? [
this.requireImport('warplib.memory', 'wm_index_dyn'),
this.requireImport('warplib.maths.utils', 'felt_to_uint256'),
]
? [this.requireImport(...WM_INDEX_DYN), this.requireImport(...FELT_TO_UINT256)]
: [];

const genFuncInfo = { name, code, functionsCalled: [...importedFuncs, ...funcCalls, readFunc] };
Expand All @@ -242,6 +247,6 @@ export class AbiEncodePacked extends AbiBase {
const funcName =
size === 32 ? 'fixed_bytes256_to_felt_dynamic_array' : `fixed_bytes_to_felt_dynamic_array`;

return this.requireImport('warplib.dynamic_arrays_util', funcName);
return this.requireImport(DYNAMIC_ARRAYS_UTIL, funcName);
}
}
23 changes: 16 additions & 7 deletions src/cairoUtilFuncGen/abi/abiEncodeWithSelector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,15 @@ import { CairoFunctionDefinition } from '../../export';
import { printTypeNode } from '../../utils/astPrinter';
import { CairoType, TypeConversionContext } from '../../utils/cairoTypeSystem';
import { TranspileFailedError } from '../../utils/errors';
import {
ALLOC,
FELT_ARRAY_TO_WARP_MEMORY_ARRAY,
FELT_TO_UINT256,
FIXED_BYTES256_TO_FELT_DYNAMIC_ARRAY,
UINT256,
WARP_KECCAK,
WM_NEW,
} from '../../utils/importPaths';
import { getByteSize } from '../../utils/nodeTypeProcessing';
import { uint256 } from '../../warplib/utils';
import { GeneratedFunctionInfo } from '../base';
Expand Down Expand Up @@ -82,13 +91,13 @@ export class AbiEncodeWithSelector extends AbiBase {
].join('\n');

const importedFuncs = [
this.requireImport('starkware.cairo.common.uint256', 'Uint256'),
this.requireImport('starkware.cairo.common.alloc', 'alloc'),
this.requireImport('warplib.maths.utils', 'felt_to_uint256'),
this.requireImport('warplib.memory', 'wm_new'),
this.requireImport('warplib.dynamic_arrays_util', 'felt_array_to_warp_memory_array'),
this.requireImport('warplib.dynamic_arrays_util', 'fixed_bytes_to_felt_dynamic_array'),
this.requireImport('warplib.keccak', 'warp_keccak'),
this.requireImport(...UINT256),
this.requireImport(...ALLOC),
this.requireImport(...FELT_TO_UINT256),
this.requireImport(...WM_NEW),
this.requireImport(...FELT_ARRAY_TO_WARP_MEMORY_ARRAY),
this.requireImport(...FIXED_BYTES256_TO_FELT_DYNAMIC_ARRAY),
this.requireImport(...WARP_KECCAK),
];

const funcInfo = {
Expand Down
Loading