Skip to content

Instantly share code, notes, and snippets.

@PsichiX
Last active August 9, 2016 07:08
Show Gist options
  • Save PsichiX/37899a92b6493f18c22e to your computer and use it in GitHub Desktop.
Save PsichiX/37899a92b6493f18c22e to your computer and use it in GitHub Desktop.
Intuicio4 Assembly Script - design v1.0
#!/usr/bin/env i4s
#intuicio 4.0;
#stack 8k;
#memory 16k;
#entry @Main;
#pointersize 32;
#import from [IO:print] routine print(format:*i8, ...):;
routine Add():
<i8:i8, i16:i16, i32:i32, i64:i64, u8:u8, u16:u16, u32:u32, u64:u64, f32:f32, f64:f64>
{
call @print("=== ADD ===\n":*i8);
add void 5:i8 -3:i8 => $i8;
call @print("i8: ${i8}\n":*i8, $i8);
add void 5:i16 -3:i16 => $i16;
call @print("i16: ${i16}\n":*i8, $i16);
add void 5:i32 -3:i32 => $i32;
call @print("i32: ${i32}\n":*i8, $i32);
add void 5:i64 -3:i64 => $i64;
call @print("i64: ${i64}\n":*i8, $i64);
add void 5:u8 3:u8 => $u8;
call @print("u8: ${u8}\n":*i8, $u8);
add void 5:u16 3:u16 => $u16;
call @print("u16: ${u16}\n":*i8, $u16);
add void 5:u32 3:u32 => $u32;
call @print("u32: ${u32}\n":*i8, $u32);
add void 5:u64 3:u64 => $u64;
call @print("u64: ${u64}\n":*i8, $u64);
add void 5.0:f32 3.0:f32 => $f32;
call @print("f32: ${f32}\n":*i8, $f32);
add void 5.0:f64 3.0:f64 => $f64;
call @print("f64: ${f64}\n":*i8, $f64);
};
routine Sub():
<i8:i8, i16:i16, i32:i32, i64:i64, u8:u8, u16:u16, u32:u32, u64:u64, f32:f32, f64:f64>
{
call @print("=== SUB ===\n":*i8);
sub void 5:i8 -3:i8 => $i8;
call @print("i8: ${i8}\n":*i8, $i8);
sub void 5:i16 -3:i16 => $i16;
call @print("i16: ${i16}\n":*i8, $i16);
sub void 5:i32 -3:i32 => $i32;
call @print("i32: ${i32}\n":*i8, $i32);
sub void 5:i64 -3:i64 => $i64;
call @print("i64: ${i64}\n":*i8, $i64);
sub void 5:u8 3:u8 => $u8;
call @print("u8: ${u8}\n":*i8, $u8);
sub void 5:u16 3:u16 => $u16;
call @print("u16: ${u16}\n":*i8, $u16);
sub void 5:u32 3:u32 => $u32;
call @print("u32: ${u32}\n":*i8, $u32);
sub void 5:u64 3:u64 => $u64;
call @print("u64: ${u64}\n":*i8, $u64);
sub void 5.0:f32 3.0:f32 => $f32;
call @print("f32: ${f32}\n":*i8, $f32);
sub void 5.0:f64 3.0:f64 => $f64;
call @print("f64: ${f64}\n":*i8, $f64);
};
routine Mul():
<i8:i8, i16:i16, i32:i32, i64:i64, u8:u8, u16:u16, u32:u32, u64:u64, f32:f32, f64:f64>
{
call @print("=== MUL ===\n":*i8);
mul void 5:i8 -3:i8 => $i8;
call @print("i8: ${i8}\n":*i8, $i8);
mul void 5:i16 -3:i16 => $i16;
call @print("i16: ${i16}\n":*i8, $i16);
mul void 5:i32 -3:i32 => $i32;
call @print("i32: ${i32}\n":*i8, $i32);
mul void 5:i64 -3:i64 => $i64;
call @print("i64: ${i64}\n":*i8, $i64);
mul void 5:u8 3:u8 => $u8;
call @print("u8: ${u8}\n":*i8, $u8);
mul void 5:u16 3:u16 => $u16;
call @print("u16: ${u16}\n":*i8, $u16);
mul void 5:u32 3:u32 => $u32;
call @print("u32: ${u32}\n":*i8, $u32);
mul void 5:u64 3:u64 => $u64;
call @print("u64: ${u64}\n":*i8, $u64);
mul void 5.0:f32 3.0:f32 => $f32;
call @print("f32: ${f32}\n":*i8, $f32);
mul void 5.0:f64 3.0:f64 => $f64;
call @print("f64: ${f64}\n":*i8, $f64);
};
routine Div():
<i8:i8, i16:i16, i32:i32, i64:i64, u8:u8, u16:u16, u32:u32, u64:u64, f32:f32, f64:f64>
{
call @print("=== DIV ===\n":*i8);
div void 5:i8 -3:i8 => $i8;
call @print("i8: ${i8}\n":*i8, $i8);
div void 5:i16 -3:i16 => $i16;
call @print("i16: ${i16}\n":*i8, $i16);
div void 5:i32 -3:i32 => $i32;
call @print("i32: ${i32}\n":*i8, $i32);
div void 5:i64 -3:i64 => $i64;
call @print("i64: ${i64}\n":*i8, $i64);
div void 5:u8 3:u8 => $u8;
call @print("u8: ${u8}\n":*i8, $u8);
div void 5:u16 3:u16 => $u16;
call @print("u16: ${u16}\n":*i8, $u16);
div void 5:u32 3:u32 => $u32;
call @print("u32: ${u32}\n":*i8, $u32);
div void 5:u64 3:u64 => $u64;
call @print("u64: ${u64}\n":*i8, $u64);
div void 5.0:f32 3.0:f32 => $f32;
call @print("f32: ${f32}\n":*i8, $f32);
div void 5.0:f64 3.0:f64 => $f64;
call @print("f64: ${f64}\n":*i8, $f64);
};
routine And():
<i8:i8, i16:i16, i32:i32, i64:i64, u8:u8, u16:u16, u32:u32, u64:u64, f32:f32, f64:f64>
{
call @print("=== AND ===\n":*i8);
and void 5:i8 -3:i8 => $i8;
call @print("i8: ${i8}\n":*i8, $i8);
and void 5:i16 -3:i16 => $i16;
call @print("i16: ${i16}\n":*i8, $i16);
and void 5:i32 -3:i32 => $i32;
call @print("i32: ${i32}\n":*i8, $i32);
and void 5:i64 -3:i64 => $i64;
call @print("i64: ${i64}\n":*i8, $i64);
and void 5:u8 3:u8 => $u8;
call @print("u8: ${u8}\n":*i8, $u8);
and void 5:u16 3:u16 => $u16;
call @print("u16: ${u16}\n":*i8, $u16);
and void 5:u32 3:u32 => $u32;
call @print("u32: ${u32}\n":*i8, $u32);
and void 5:u64 3:u64 => $u64;
call @print("u64: ${u64}\n":*i8, $u64);
};
routine Or():
<i8:i8, i16:i16, i32:i32, i64:i64, u8:u8, u16:u16, u32:u32, u64:u64, f32:f32, f64:f64>
{
call @print("=== OR ===\n":*i8);
or void 5:i8 -3:i8 => $i8;
call @print("i8: ${i8}\n":*i8, $i8);
or void 5:i16 -3:i16 => $i16;
call @print("i16: ${i16}\n":*i8, $i16);
or void 5:i32 -3:i32 => $i32;
call @print("i32: ${i32}\n":*i8, $i32);
or void 5:i64 -3:i64 => $i64;
call @print("i64: ${i64}\n":*i8, $i64);
or void 5:u8 3:u8 => $u8;
call @print("u8: ${u8}\n":*i8, $u8);
or void 5:u16 3:u16 => $u16;
call @print("u16: ${u16}\n":*i8, $u16);
or void 5:u32 3:u32 => $u32;
call @print("u32: ${u32}\n":*i8, $u32);
or void 5:u64 3:u64 => $u64;
call @print("u64: ${u64}\n":*i8, $u64);
};
routine Xor():
<i8:i8, i16:i16, i32:i32, i64:i64, u8:u8, u16:u16, u32:u32, u64:u64, f32:f32, f64:f64>
{
call @print("=== XOR ===\n":*i8);
xor void 5:i8 -3:i8 => $i8;
call @print("i8: ${i8}\n":*i8, $i8);
xor void 5:i16 -3:i16 => $i16;
call @print("i16: ${i16}\n":*i8, $i16);
xor void 5:i32 -3:i32 => $i32;
call @print("i32: ${i32}\n":*i8, $i32);
xor void 5:i64 -3:i64 => $i64;
call @print("i64: ${i64}\n":*i8, $i64);
xor void 5:u8 3:u8 => $u8;
call @print("u8: ${u8}\n":*i8, $u8);
xor void 5:u16 3:u16 => $u16;
call @print("u16: ${u16}\n":*i8, $u16);
xor void 5:u32 3:u32 => $u32;
call @print("u32: ${u32}\n":*i8, $u32);
xor void 5:u64 3:u64 => $u64;
call @print("u64: ${u64}\n":*i8, $u64);
};
routine Lsh():
<i8:i8, i16:i16, i32:i32, i64:i64, u8:u8, u16:u16, u32:u32, u64:u64, f32:f32, f64:f64>
{
call @print("=== LSH ===\n":*i8);
lsh void 5:i8 -3:i8 => $i8;
call @print("i8: ${i8}\n":*i8, $i8);
lsh void 5:i16 -3:i16 => $i16;
call @print("i16: ${i16}\n":*i8, $i16);
lsh void 5:i32 -3:i32 => $i32;
call @print("i32: ${i32}\n":*i8, $i32);
lsh void 5:i64 -3:i64 => $i64;
call @print("i64: ${i64}\n":*i8, $i64);
lsh void 5:u8 3:u8 => $u8;
call @print("u8: ${u8}\n":*i8, $u8);
lsh void 5:u16 3:u16 => $u16;
call @print("u16: ${u16}\n":*i8, $u16);
lsh void 5:u32 3:u32 => $u32;
call @print("u32: ${u32}\n":*i8, $u32);
lsh void 5:u64 3:u64 => $u64;
call @print("u64: ${u64}\n":*i8, $u64);
};
routine Rsh():
<i8:i8, i16:i16, i32:i32, i64:i64, u8:u8, u16:u16, u32:u32, u64:u64, f32:f32, f64:f64>
{
call @print("=== RSH ===\n":*i8);
rsh void 5:i8 -3:i8 => $i8;
call @print("i8: ${i8}\n":*i8, $i8);
rsh void 5:i16 -3:i16 => $i16;
call @print("i16: ${i16}\n":*i8, $i16);
rsh void 5:i32 -3:i32 => $i32;
call @print("i32: ${i32}\n":*i8, $i32);
rsh void 5:i64 -3:i64 => $i64;
call @print("i64: ${i64}\n":*i8, $i64);
rsh void 5:u8 3:u8 => $u8;
call @print("u8: ${u8}\n":*i8, $u8);
rsh void 5:u16 3:u16 => $u16;
call @print("u16: ${u16}\n":*i8, $u16);
rsh void 5:u32 3:u32 => $u32;
call @print("u32: ${u32}\n":*i8, $u32);
rsh void 5:u64 3:u64 => $u64;
call @print("u64: ${u64}\n":*i8, $u64);
};
routine Eq():
<i8:i8, i16:i16, i32:i32, i64:i64, u8:u8, u16:u16, u32:u32, u64:u64, f32:f32, f64:f64>
{
call @print("=== EQ ===\n":*i8);
call @print("--- false ---\n":*i8);
eq void 5:i8 -3:i8 => $i8;
call @print("i8: ${i8}\n":*i8, $i8);
eq void 5:i16 -3:i16 => $i16;
call @print("i16: ${i16}\n":*i8, $i16);
eq void 5:i32 -3:i32 => $i32;
call @print("i32: ${i32}\n":*i8, $i32);
eq void 5:i64 -3:i64 => $i64;
call @print("i64: ${i64}\n":*i8, $i64);
eq void 5:u8 3:u8 => $u8;
call @print("u8: ${u8}\n":*i8, $u8);
eq void 5:u16 3:u16 => $u16;
call @print("u16: ${u16}\n":*i8, $u16);
eq void 5:u32 3:u32 => $u32;
call @print("u32: ${u32}\n":*i8, $u32);
eq void 5:u64 3:u64 => $u64;
call @print("u64: ${u64}\n":*i8, $u64);
eq void 5.0:f32 3.0:f32 => $f32;
call @print("f32: ${f32}\n":*i8, $f32);
eq void 5.0:f64 3.0:f64 => $f64;
call @print("f64: ${f64}\n":*i8, $f64);
call @print("--- true ---\n":*i8);
eq void 5:i8 5:i8 => $i8;
call @print("i8: ${i8}\n":*i8, $i8);
eq void 5:i16 5:i16 => $i16;
call @print("i16: ${i16}\n":*i8, $i16);
eq void 5:i32 5:i32 => $i32;
call @print("i32: ${i32}\n":*i8, $i32);
eq void 5:i64 5:i64 => $i64;
call @print("i64: ${i64}\n":*i8, $i64);
eq void 5:u8 5:u8 => $u8;
call @print("u8: ${u8}\n":*i8, $u8);
eq void 5:u16 5:u16 => $u16;
call @print("u16: ${u16}\n":*i8, $u16);
eq void 5:u32 5:u32 => $u32;
call @print("u32: ${u32}\n":*i8, $u32);
eq void 5:u64 5:u64 => $u64;
call @print("u64: ${u64}\n":*i8, $u64);
eq void 5.0:f32 5.0:f32 => $f32;
call @print("f32: ${f32}\n":*i8, $f32);
eq void 5.0:f64 5.0:f64 => $f64;
call @print("f64: ${f64}\n":*i8, $f64);
};
routine Ls():
<i8:i8, i16:i16, i32:i32, i64:i64, u8:u8, u16:u16, u32:u32, u64:u64, f32:f32, f64:f64>
{
call @print("=== LS ===\n":*i8);
call @print("--- false ---\n":*i8);
ls void 5:i8 -3:i8 => $i8;
call @print("i8: ${i8}\n":*i8, $i8);
ls void 5:i16 -3:i16 => $i16;
call @print("i16: ${i16}\n":*i8, $i16);
ls void 5:i32 -3:i32 => $i32;
call @print("i32: ${i32}\n":*i8, $i32);
ls void 5:i64 -3:i64 => $i64;
call @print("i64: ${i64}\n":*i8, $i64);
ls void 5:u8 3:u8 => $u8;
call @print("u8: ${u8}\n":*i8, $u8);
ls void 5:u16 3:u16 => $u16;
call @print("u16: ${u16}\n":*i8, $u16);
ls void 5:u32 3:u32 => $u32;
call @print("u32: ${u32}\n":*i8, $u32);
ls void 5:u64 3:u64 => $u64;
call @print("u64: ${u64}\n":*i8, $u64);
ls void 5.0:f32 -3.0:f32 => $f32;
call @print("f32: ${f32}\n":*i8, $f32);
ls void 5.0:f64 3.0:f64 => $f64;
call @print("f64: ${f64}\n":*i8, $f64);
call @print("--- true ---\n":*i8);
ls void -5:i8 3:i8 => $i8;
call @print("i8: ${i8}\n":*i8, $i8);
ls void -5:i16 3:i16 => $i16;
call @print("i16: ${i16}\n":*i8, $i16);
ls void -5:i32 3:i32 => $i32;
call @print("i32: ${i32}\n":*i8, $i32);
ls void -5:i64 3:i64 => $i64;
call @print("i64: ${i64}\n":*i8, $i64);
ls void 3:u8 5:u8 => $u8;
call @print("u8: ${u8}\n":*i8, $u8);
ls void 3:u16 5:u16 => $u16;
call @print("u16: ${u16}\n":*i8, $u16);
ls void 3:u32 5:u32 => $u32;
call @print("u32: ${u32}\n":*i8, $u32);
ls void 3:u64 5:u64 => $u64;
call @print("u64: ${u64}\n":*i8, $u64);
ls void -5.0:f32 3.0:f32 => $f32;
call @print("f32: ${f32}\n":*i8, $f32);
ls void -5.0:f64 3.0:f64 => $f64;
call @print("f64: ${f64}\n":*i8, $f64);
};
routine Gt():
<i8:i8, i16:i16, i32:i32, i64:i64, u8:u8, u16:u16, u32:u32, u64:u64, f32:f32, f64:f64>
{
call @print("=== GT ===\n":*i8);
call @print("--- true ---\n":*i8);
ls void -5:i8 3:i8 => $i8;
call @print("i8: ${i8}\n":*i8, $i8);
ls void -5:i16 3:i16 => $i16;
call @print("i16: ${i16}\n":*i8, $i16);
ls void -5:i32 3:i32 => $i32;
call @print("i32: ${i32}\n":*i8, $i32);
ls void -5:i64 3:i64 => $i64;
call @print("i64: ${i64}\n":*i8, $i64);
ls void 3:u8 5:u8 => $u8;
call @print("u8: ${u8}\n":*i8, $u8);
ls void 3:u16 5:u16 => $u16;
call @print("u16: ${u16}\n":*i8, $u16);
ls void 3:u32 5:u32 => $u32;
call @print("u32: ${u32}\n":*i8, $u32);
ls void 3:u64 5:u64 => $u64;
call @print("u64: ${u64}\n":*i8, $u64);
ls void -5.0:f32 3.0:f32 => $f32;
call @print("f32: ${f32}\n":*i8, $f32);
ls void -5.0:f64 3.0:f64 => $f64;
call @print("f64: ${f64}\n":*i8, $f64);
call @print("--- false ---\n":*i8);
ls void 5:i8 -3:i8 => $i8;
call @print("i8: ${i8}\n":*i8, $i8);
ls void 5:i16 -3:i16 => $i16;
call @print("i16: ${i16}\n":*i8, $i16);
ls void 5:i32 -3:i32 => $i32;
call @print("i32: ${i32}\n":*i8, $i32);
ls void 5:i64 -3:i64 => $i64;
call @print("i64: ${i64}\n":*i8, $i64);
ls void 5:u8 3:u8 => $u8;
call @print("u8: ${u8}\n":*i8, $u8);
ls void 5:u16 3:u16 => $u16;
call @print("u16: ${u16}\n":*i8, $u16);
ls void 5:u32 3:u32 => $u32;
call @print("u32: ${u32}\n":*i8, $u32);
ls void 5:u64 3:u64 => $u64;
call @print("u64: ${u64}\n":*i8, $u64);
ls void 5.0:f32 -3.0:f32 => $f32;
call @print("f32: ${f32}\n":*i8, $f32);
ls void 5.0:f64 3.0:f64 => $f64;
call @print("f64: ${f64}\n":*i8, $f64);
};
routine arithmetics():
{
call @Add();
call @Sub();
call @Mul();
call @Div();
call @And();
call @Or();
call @Xor();
call @Lsh();
call @Rsh();
call @Eq();
call @Ls();
call @Gt();
};
routine Main():i32
{
call @arithmetics();
ret 0:i32;
};
#!/usr/bin/env i4s
#intuicio 4.0;
#stack 8k;
#memory 16k;
#entry @Main;
#pointersize 32;
#import from [IO:print] routine print(format:*i8, ...):;
export synchronized routine multiply(a:i32, b:i32):i32
<result:i32>
{
call @print("Hello module!\n":*i8);
mul void $a $b => $result;
ret $result;
};
routine Main():i32
{
ret 0:i32;
};
#!/usr/bin/env i4s
#intuicio 4.0;
// routine with VARG feature.
synchronized routine sum(...):i32
<result:i32, temp:i32, next:i32>
{
mov void 0:i32 => $result;
// store VARG list stating position.
vargs => $next;
test:
// if position is 0, then return result,
jif $next %read %return;
read:
// otherwise get VARG value.
vargr $next => $temp $next;
// and add to result.
add void $result $temp => $result;
// then go back to position testing.
goto %test;
return:
ret $result;
};
#!/usr/bin/env i4s
#intuicio 4.0; // intuicio assembly header with version.
#stack 8k; // application stack size in bytes.
//#memory 32k; // application memory limit. 0 means allocatons are done on system memory.
#entry @Main; // entry point (name of routine that will be called first).
#pointersize 32; // tell compiler how many bits in size is pointer.
//#import from ? struct Hello;
#import from [IO:print] routine print(format:*i8, ...):; // import routine from native module.
#import from "std.i4s" routine sum(...):i32; // import routine from file.
#import from [module:multiply] routine multiply(a:i32, b:i32):i32; // import routine from script module.
#import from [IO:file_exists] routine file_exists(path:*i8):i32;
#import from [IO:file_open] routine file_open(path:*i8, mode:*i8):*i32;
#import from [IO:file_close] routine file_close(handle:*i32):;
#import from [IO:file_size] routine file_size(handle:*i32):i32;
#import from [IO:file_read] routine file_read(handle:*i32, buffer:*i8, size:i32):i32;
#import from [IO:file_write] routine file_write(handle:*i32, buffer:*i8, size:i32):i32;
#import from "arithmetics.i4s" routine arithmetics():;
// global variables.
<
g_accum:i32,
m_AsyncLoop:*i32
>;
// define structure:
struct Person
{
age:i32;
weight:f32;
name:*i8;
};
routine Person_Constructor(this:*Person, age:i32, weight:f32, name:*i8):
{
mov void $age => $this->$age;
mov void $weight => $this->$weight;
mov void $name => $this->$name;
ret void; // if we want to return without value, use void as value pointer.
};
routine Person_Destructor(this:*Person):
{
mov void 0:i32 => $this->$age;
mov void 0.0:f32 => $this->$weight;
mov void null => $this->$name;
};
export routine CreatePerson(age:i32, weight:f32, name:*i8):*Person
<result:*Person>
{
new Person 1:i32 => $result;
call @Person_Constructor($result, $age, $weight, $name);
ret $result;
};
export routine DestroyPerson(ptr:*Person):
{
jif $ptr %success %failure;
success:
call @Person_Destructor($ptr);
del $ptr;
failure:
};
synchronized routine AsyncReadFile(path:*i8):
<file:*i32, buffer:*i8, v:i32>
{
call @file_exists($path) => $v;
jif $v %success %failure;
success:
call @print("File path: ${*}\n":*i8, $path);
call @file_open($path, "r+":*i8) => $file;
call @print("File handle: ${i32}\n":*i8, $file);
call @file_size($file) => $v;
call @print("File size: ${i32}\n":*i8, $v);
new i8 $v => $buffer;
call @file_read($file, $buffer, $v) => $v;
call @print("File contents:\n${*}\n":*i8, $buffer);
call @file_close($file);
del $buffer;
ret void;
failure:
call @print("Could not find under path: ${*}\n":*i8, $path);
};
struct CoroutineState
{
context:i32;
operation:i32;
hasnext:i8;
repeats:i32;
};
routine Countdown(state:*CoroutineState):
{
// first we have to determine if it's first iteration of coroutine.
jif $state->$hasnext %resume %loop;
resume:
// if it's not, then we have to resume execution from last point.
jump $state->$operation;
loop:
// in loop we check if value to print is not equals 0.
jif $state->$repeats %print %exit;
print:
// if it's not, then print value and decrement counter.
call @print("Countdown: ${i32}!\n":*i8, $state->$repeats);
sub void $state->$repeats 1:i32 => $state->$repeats;
// here is typical YIELD statement code.
// first we have to store address of continuation point in routine.
address %loop => $state->$operation;
// then set coroutine continuation flag to true.
mov void 1:i8 => $state->$hasnext;
// and just return from routine.
ret void;
exit:
// here we execute final code, outside from loop.
call @print("Kaboom!\n":*i8);
// and set coroutine continuation flag to false,
// so it will not continue further.
mov void 0:i8 => $state->$hasnext;
};
// example of routine `synchronized` feature done by direct lock/unlock operations.
routine Accum(v:i32):
{
lock $m_AsyncLoop;
add void $v $g_accum => $g_accum;
unlock $m_AsyncLoop;
};
routine getNull():*Person
{
ret null;
};
routine CalculateStringLength(value:*i8):i32
<pos:i32, char:i8>
{
mov void -1:i32 => $pos;
iterate:
add void 1:i32 $pos => $pos;
val $value => $char;
add void $value 1:i32 => $value;
jif $char %iterate %done;
done:
ret $pos;
};
// routine name with arguments and returned value type declaration.
routine Main():i32
// routine variables declaration.
<pA:*Person, pB:*Person, v:i32, thread:i32, at0:i32, at1:i32, state:*CoroutineState, stack:i32, ci:i32, cf:f32, cs:*i8>
// routine prologue and body:
{
call @CreatePerson(20:i32, 60.75:f32, "John":*i8) => $pA;
call @CreatePerson(18:i32, 51.5:f32, "Emilly":*i8) => $pB;
add void $pA->$age $pB->$age => $v;
jif $v %success %return;
success: // label that marks jump space used in jif operation
sub void $pA->$age $pB->$age => $v;
call @sum($v, $pA->$age, $pB->$age) => $v;
goto %return;
return:
call @print("Sum of ages is: ${i32}\n":*i8, $v);
call @multiply($pA->$age, $pB->$age) => $v;
call @print("Mul of ages is: ${i32}\n":*i8, $v);
async @AsyncReadFile("../tests/std.i4s":*i8) => $thread;
call @print("Async call for AsyncFileRead() on thread: ${i32}\n":*i8, $thread);
mtxc => $m_AsyncLoop;
mov void 0:i32 => $g_accum;
async @Accum(15:i32) => $at0;
async @Accum(-3:i32) => $at1;
join $at0;
join $at1;
call @print("Accum is: ${i32}\n":*i8, $g_accum);
call @arithmetics();
ctxc => $stack;
cpush $stack void 1:i32;
cpush $stack void 2.3:f32;
cpush $stack void "hello":*i8;
call @print("====== START OF COROUTINE:\n":*i8);
// create coroutine state object.
new CoroutineState 1:i32 => $state;
// create execution context (stack) for coroutine.
// it creates a clone of currently active stack.
ctxc => $state->$context;
// reset coroutine state.
mov void 0:i8 => $state->$hasnext;
// setup initial values.
mov void 5:i32 => $state->$repeats;
loop:
// switch to coroutine context.
ctxs $state->$context;
// call coroutine bound to it's own context.
call @Countdown($state);
// reset current context to one before context switching.
ctxs 0:i32;
// test if there is a next iteration possible for given context and repeat.
jif $state->$hasnext %loop %exit;
exit:
// after coroutine execution complete destroy it's context.
call @print("====== END OF COROUTINE!\n":*i8);
cpop $stack void => $cs;
cpop $stack void => $cf;
cpop $stack void => $ci;
call @print("Context pop integer: ${i32}\n":*i8, $ci);
call @print("Context pop float: ${f32}\n":*i8, $cf);
call @print("Context pop string: \"${*}\"\n":*i8, $cs);
call @getNull() => $pA;
call @CalculateStringLength("Hello World!\0":*i8) => $ci;
call @print("'Hello World!' string length: ${i32}\n":*i8, $ci);
ret 0:i32;
}
// routine epilogue: instructions stored here will be always executed on exit from routine.
[{
call @DestroyPerson($pA);
call @DestroyPerson($pB);
join $thread;
mtxd $m_AsyncLoop;
push void -35:i32;
drop i32;
push void 17:i32;
pop void => $v;
call @print("Value restored from stack is: ${i32}\n":*i8, $v);
ctxd $stack;
ctxd $state->$context;
del $state;
}];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment