Skip to content

Instantly share code, notes, and snippets.

@UplinkCoder
Last active December 9, 2016 22:37
Show Gist options
  • Select an option

  • Save UplinkCoder/1b7eb4f84f0d055c8281fa9ac6d2f591 to your computer and use it in GitHub Desktop.

Select an option

Save UplinkCoder/1b7eb4f84f0d055c8281fa9ac6d2f591 to your computer and use it in GitHub Desktop.
import std.datetime : StopWatch;
import std.stdio;
import ddmd.ctfe.bc_common;
import ddmd.ctfe.bc;
import core.stdc.string;
__gshared int[4096] bca;
__gshared BCGen gen;
uint makeFirst()
{
StopWatch sw;
sw.start;
with (gen)
{
Initialize();
auto p1 = genParameter(BCType(BCTypeEnum.String));//SP[4]
auto p2 = genParameter(BCType(BCTypeEnum.String));//SP[8]
auto p3 = genParameter(BCType(BCTypeEnum.String));//SP[12]
beginFunction();//fn(1
auto tmp1 = genTemporary(BCType(BCTypeEnum.i32));//SP[16]
p1 = p1.i32;
p2 = p2.i32;
p3 = p3.i32;
Load32(tmp1, p2);
Gt3(BCValue.init, tmp1, BCValue(Imm32(2)));
AssertError(BCValue.init, BCValue(Imm32(1)/*Error*/));
auto tmp2 = genTemporary(BCType(BCTypeEnum.i32));//SP[20]
auto tmp3 = genTemporary(BCType(BCTypeEnum.Char));//SP[24]
auto tmp4 = genTemporary(BCType(BCTypeEnum.i32));//SP[28]
Add3(tmp2, p2, BCValue(Imm32(1)));
auto tmp5 = genTemporary(BCType(BCTypeEnum.i32));//SP[32]
Mod3(tmp5, BCValue(Imm32(2)), BCValue(Imm32(4)));
Div3(tmp4, BCValue(Imm32(2)), BCValue(Imm32(4)));
Add3(tmp2, tmp2, tmp4);
Load32(tmp3, tmp2);
Byte3(tmp3, tmp3, tmp5);
Ret(tmp3);
endFunction();
Finalize();
memcpy(&bca[0], &gen.byteCodeArray[0], gen.ip * uint.sizeof);
sw.stop;
// writeln(bca[0 .. gen.ip].printInstructions);
}
return cast(uint)sw.peek.nsecs;
}
void main()
{
import std.stdio;
writeln ("makeing the first bytecode took ", makeFirst(), "nsecs");
foreach (n;0 .. 2)writeln ("makeing the ",n," bytecode took ", makeFirst(), "nsecs");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment