Пример #1
0
int main(int argc, char* argv[])
{
  using namespace AsmJit;

  // ==========================================================================
  // Create compiler.
  Compiler c;

  // Log compiler output.
  FileLogger logger(stderr);
  c.setLogger(&logger);

  ECall* ctx;
  Label skip(c.newLabel());

  EFunction* func = c.newFunction(CALL_CONV_DEFAULT, FunctionBuilder1<int, int>());
  func->setHint(FUNCTION_HINT_NAKED, true);

  GPVar var(c.argGP(0));
  c.cmp(var, imm(1));
  c.jle(skip);

  GPVar tmp(c.newGP(VARIABLE_TYPE_INT32));
  c.mov(tmp, var);
  c.dec(tmp);

  ctx = c.call(func->getEntryLabel());
  ctx->setPrototype(CALL_CONV_DEFAULT, FunctionBuilder1<int, int>());
  ctx->setArgument(0, tmp);
  ctx->setReturn(tmp);
  c.mul_lo_hi(var, c.newGP(VARIABLE_TYPE_INT32), tmp);

  c.bind(skip);
  c.ret(var);
  c.endFunction();
  // ==========================================================================

  // ==========================================================================
  // Make the function.
  MyFn fn = function_cast<MyFn>(c.make());

  printf("Factorial 5 == %d\n", fn(5));

  // Free the generated function if it's not needed anymore.
  MemoryManager::getGlobal()->free((void*)fn);
  // ==========================================================================

  return 0;
}
Пример #2
0
int main(int argc, char* argv[])
{
  using namespace AsmJit;

  // ==========================================================================
  // Create compiler.
  Compiler c;

  // Log compiler output.
  FileLogger logger(stderr);
  c.setLogger(&logger);

  c.newFunction(CALL_CONV_DEFAULT, FunctionBuilder0<int>());
  c.getFunction()->setHint(FUNCTION_HINT_NAKED, true);
  GPVar x(c.newGP());
  GPVar y(c.newGP());
  ECall *ctx = c.call((void*)oneFunc);
  ctx->setPrototype(CALL_CONV_DEFAULT, FunctionBuilder0<int>());
  ctx->setReturn(y);
  c.mov(x, 0);
  c.add(x, y);
  c.ret(x);
  c.endFunction();
  // ==========================================================================

  // ==========================================================================
  // Make the function.
  MyFn fn = function_cast<MyFn>(c.make());

  uint result = fn();
  bool success = result == 1;

  printf("Result %u (expected 1)\n", result);
  printf("Status: %s\n", success ? "Success" : "Failure");

  // Free the generated function if it's not needed anymore.
  MemoryManager::getGlobal()->free((void*)fn);
  // ==========================================================================

  return 0;
}