Introduction to the ZK World Compiler
Last updated
Last updated
The ZK World compiler compiles high-level ZK World contract code into assembly code supported by ZK WorldVM. The general pipeline flow is shown in Figure 12:
As can be seen from the figure above, the front end of the compiler takes the high-level contract program as input and compiles it into LLVM intermediate representation (IR); the back end of the compiler takes the LLVM IR generated by the front end as input and compiles it into ZK World assembly code.
The assembly code is finally assembled, linked, loaded and executed by ZK WorldVM through the toolchain pipeline to generate traces.
To illustrate the compiler pipeline, here is an example of a typical u32 type sqrt operation, with two different versions of directive and oracle to show the code generation process.
A ZK World-lang high-level language program using as to calculate u32 type is following
Contract Standard Contract
Main (){
sqrt _ test (4);}
fn sqrt_ test(u32 n)- >(u32){
u32 b = u32 sqrt (n);
\36820;\ b_;
}
}
An example of a ZK World-lang high-level language program for computing sqrt of type u32 is as follows:
Contract Standard Contract
Main (){
sqrt _ test (4);
}
fn sqrt _ test(u32 a)- >(u32){
U32 result=0;
If (a>3){
Result=a;
u32 x = a / 2 + 1;
//Assuming a maximum number of iterations of 100
Is (u32 i=0; i<100; i++){
If (x>=result) interrupts;
Result=x;
x = ( a / x + x ) / 2;
}
}Otherwise, if (a!=0){
Result=1;
}
Feedback results
}}