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

uintN->uN #967

Merged
merged 19 commits into from
Apr 13, 2023
Merged
Show file tree
Hide file tree
Changes from 8 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
9 changes: 5 additions & 4 deletions src/cairoUtilFuncGen/memory/memoryRead.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,18 @@ import { CairoFunctionDefinition, typeNameFromTypeNode } from '../../export';
import {
CairoFelt,
CairoType,
CairoUint256,
CairoUint,
MemoryLocation,
TypeConversionContext,
} from '../../utils/cairoTypeSystem';
import { cloneASTNode } from '../../utils/cloning';
import { createCairoGeneratedFunction, createCallToFunction } from '../../utils/functionGeneration';
import { DICT_READ, WM_READ256, WM_READ_FELT, WM_READ_ID } from '../../utils/importPaths';
import { DICT_READ, WM_READ_FELT, WM_READ_ID } from '../../utils/importPaths';
import { createNumberLiteral, createNumberTypeName } from '../../utils/nodeTemplates';
import { isDynamicArray, safeGetNodeType } from '../../utils/nodeTypeProcessing';
import { add, GeneratedFunctionInfo, locationIfComplexType, StringIndexedFuncGen } from '../base';
import { serialiseReads } from '../serialisation';
import { wmReaduNImport } from '../utils/uNselector';

/*
Produces functions that when given a start location in warp_memory, deserialise all necessary
Expand Down Expand Up @@ -83,8 +84,8 @@ export class MemoryReadGen extends StringIndexedFuncGen {
funcDef = this.requireImport(...WM_READ_ID, inputs, outputs);
} else if (resultCairoType instanceof CairoFelt) {
funcDef = this.requireImport(...WM_READ_FELT, inputs, outputs);
} else if (resultCairoType.fullStringRepresentation === CairoUint256.fullStringRepresentation) {
funcDef = this.requireImport(...WM_READ256, inputs, outputs);
} else if (resultCairoType instanceof CairoUint) {
funcDef = this.requireImport(...wmReaduNImport(resultCairoType), inputs, outputs);
rjnrohit marked this conversation as resolved.
Show resolved Hide resolved
} else {
const funcInfo = this.getOrCreate(resultCairoType);
funcDef = createCairoGeneratedFunction(funcInfo, inputs, outputs, this.ast, this.sourceUnit, {
Expand Down
13 changes: 7 additions & 6 deletions src/cairoUtilFuncGen/memory/memoryWrite.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import { Expression, FunctionCall, TypeNode, DataLocation, PointerType } from 'solc-typed-ast';
import { CairoFelt, CairoType, CairoUint256 } from '../../utils/cairoTypeSystem';
import { CairoFelt, CairoType, CairoUint } from '../../utils/cairoTypeSystem';
import { cloneASTNode } from '../../utils/cloning';
import {
createCairoGeneratedFunction,
createCallToFunction,
ParameterInfo,
} from '../../utils/functionGeneration';
import { DICT_WRITE, WM_WRITE256, WM_WRITE_FELT } from '../../utils/importPaths';
import { DICT_WRITE, WM_WRITE_FELT } from '../../utils/importPaths';
import { safeGetNodeType } from '../../utils/nodeTypeProcessing';
import { typeNameFromTypeNode } from '../../utils/utils';
import { add, GeneratedFunctionInfo, StringIndexedFuncGen } from '../base';
import { wmWriteuNImport } from '../utils/uNselector';

/*
Produces functions to write a given value into warp_memory, returning that value (to simulate assignments)
Expand Down Expand Up @@ -49,10 +50,10 @@ export class MemoryWriteGen extends StringIndexedFuncGen {
const cairoTypeToWrite = CairoType.fromSol(typeToWrite, this.ast);
if (cairoTypeToWrite instanceof CairoFelt) {
return this.requireImport(...WM_WRITE_FELT, inputs, outputs);
} else if (
cairoTypeToWrite.fullStringRepresentation === CairoUint256.fullStringRepresentation
) {
return this.requireImport(...WM_WRITE256, inputs, outputs);
}

if (cairoTypeToWrite instanceof CairoUint) {
this.requireImport(...wmWriteuNImport(cairoTypeToWrite), inputs, outputs);
}

const funcInfo = this.getOrCreate(typeToWrite);
Expand Down
21 changes: 14 additions & 7 deletions src/cairoUtilFuncGen/serialisation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
CairoStaticArray,
CairoStruct,
CairoType,
CairoUint,
CairoUint256,
WarpLocation,
} from '../utils/cairoTypeSystem';
Expand Down Expand Up @@ -48,24 +49,30 @@ function producePackExpression(type: CairoType): (string | Read)[] {
')',
];
}
if (type instanceof CairoStruct) {

if (type instanceof CairoUint) {
if (type.fullStringRepresentation === CairoUint256.fullStringRepresentation) {
return [
type.name,
type.toString(),
'{',
...[...type.members.entries()]
...[
['low', new CairoUint(128)],
['high', new CairoUint(128)],
]
.flatMap(([memberName, memberType]) => [
memberName,
memberName as string,
':',
'u128_from_felt(',
...producePackExpression(memberType),
')',
...producePackExpression(memberType as CairoType),
',',
])
.slice(0, -1),
'}',
];
}
return [`core::integer::${type.toString()}_from_felt(${Read.Felt})`];
}

if (type instanceof CairoStruct) {
return [
type.name,
'{',
Expand Down
15 changes: 13 additions & 2 deletions src/cairoUtilFuncGen/storage/storageWrite.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
import endent from 'endent';
import { Expression, FunctionCall, TypeNode, DataLocation, PointerType } from 'solc-typed-ast';
import { CairoType, CairoUint256, TypeConversionContext } from '../../utils/cairoTypeSystem';
import {
CairoType,
CairoUint,
CairoUint256,
TypeConversionContext,
} from '../../utils/cairoTypeSystem';
import { cloneASTNode } from '../../utils/cloning';
import { createCairoGeneratedFunction, createCallToFunction } from '../../utils/functionGeneration';
import { U128_TO_FELT } from '../../utils/importPaths';
import { safeGetNodeType } from '../../utils/nodeTypeProcessing';
import { typeNameFromTypeNode } from '../../utils/utils';
import { add, GeneratedFunctionInfo, StringIndexedFuncGen } from '../base';
import { toFeltfromuXImport } from '../utils/uNselector';

export class StorageWriteGen extends StringIndexedFuncGen {
public gen(storageLocation: Expression, writeValue: Expression): FunctionCall {
Expand Down Expand Up @@ -55,11 +61,16 @@ export class StorageWriteGen extends StringIndexedFuncGen {
TypeConversionContext.StorageAllocation,
);
const cairoTypeString = cairoTypeToWrite.toString();
const fnsToImport: [string[], string][] = [];
const writeCode = cairoTypeToWrite
.serialiseMembers('value')
.map((name, index) => {
if (cairoTypeToWrite.fullStringRepresentation === CairoUint256.fullStringRepresentation) {
name = `u128_to_felt(${name})`;
fnsToImport.push(U128_TO_FELT);
} else if (cairoTypeToWrite instanceof CairoUint) {
name = `${cairoTypeString}_to_felt(${name})`;
fnsToImport.push(toFeltfromuXImport(cairoTypeToWrite));
}
return ` ${write(add('loc', index), name)}`;
})
Expand All @@ -74,7 +85,7 @@ export class StorageWriteGen extends StringIndexedFuncGen {
return value;
}
`,
functionsCalled: [this.requireImport(...U128_TO_FELT)],
functionsCalled: [...fnsToImport.map((imp) => this.requireImport(...imp))],
};
return funcInfo;
}
Expand Down
Loading