Example #1
0
static const Proto* combine(lua_State* L, int n)
{
 if (n==1)
  return toproto(L,-1);
 else
 {
  int i,pc;
  Proto* f=luaF_newproto(L);
  setptvalue2s(L,L->top,f); incr_top(L);
  f->source=luaS_newliteral(L,"=(" PROGNAME ")");
  f->maxstacksize=1;
  pc=2*n+1;
  f->code=luaM_newvector(L,pc,Instruction);
  f->sizecode=pc;
  f->p=luaM_newvector(L,n,Proto*);
  f->sizep=n;
  pc=0;
  for (i=0; i<n; i++)
  {
   f->p[i]=toproto(L,i-n-1);
   f->code[pc++]=CREATE_ABx(OP_CLOSURE,0,i);
   f->code[pc++]=CREATE_ABC(OP_CALL,0,1,1);
  }
  f->code[pc++]=CREATE_ABC(OP_RETURN,0,1,0);
  return f;
 }
}
static Proto* combine(lua_State* L, int n)
{
	if (n==1)
	{
		const Closure* c=(const Closure*)lua_topointer(L,-1);
		return c->l.p;
	}
	else
	{
		int i,pc=0;
		Proto* f=luaF_newproto(L);
		f->source=luaS_newliteral(L,"=(none)");
		f->maxstacksize=1;
		f->p=luaM_newvector(L,n,Proto*);
		f->sizep=n;
		f->sizecode=2*n+1;
		f->code=luaM_newvector(L,f->sizecode,Instruction);
		for (i=0; i<n; i++)
		{
			const Closure* c=(const Closure*)lua_topointer(L,i-n);
			f->p[i]=c->l.p;
			f->code[pc++]=CREATE_ABx(OP_CLOSURE,0,i);
			f->code[pc++]=CREATE_ABC(OP_CALL,0,1,1);
		}
		f->code[pc++]=CREATE_ABC(OP_RETURN,0,1,0);
		return f;
	}
}
static int lua_combine( lua_State* L) {
  int n = lua_gettop( L); /* Number of functions to combine */
  if( 1 == n) {
    return 1; /* Only one function, nothing to combine */
  } else {
      int i, pc = 3*n + 1;
      Proto* f = luaF_newproto( L);
      setptvalue2s( L,L->top,f); 
      incr_top( L);
      f->source       = luaS_newliteral( L,"=(combiner)");
      f->maxstacksize = 2;
      f->is_vararg    = VARARG_ISVARARG;
      f->code         = luaM_newvector(L, pc, Instruction);
      f->sizecode     = pc;
      f->p            = luaM_newvector( L, n, Proto*);
      f->sizep        = n;
      for( i = pc = 0; i < n; i ++) {
        int proto_idx = i-n-1;
        Proto *p      = clvalue( L->top + proto_idx)->l.p;
        f->p[i]       = p;
        f->code[pc++] = CREATE_ABx( OP_CLOSURE, 0, i);
        f->code[pc++] = CREATE_ABx( OP_VARARG,  1, 0);
        f->code[pc++] = CREATE_ABC( OP_CALL,    0, 0, 1);
      }
      f->code[pc++]   = CREATE_ABC( OP_RETURN, 0, 1, 0);
      return 1;
    }
}
Example #4
0
/*
** Format and emit an 'iABC' instruction. (Assertions check consistency
** of parameters versus opcode.)
*/
int luaK_codeABC (FuncState *fs, OpCode o, int a, int b, int c) {
  lua_assert(getOpMode(o) == iABC);
  lua_assert(getBMode(o) != OpArgN || b == 0);
  lua_assert(getCMode(o) != OpArgN || c == 0);
  lua_assert(a <= MAXARG_A && b <= MAXARG_B && c <= MAXARG_C);
  return luaK_code(fs, CREATE_ABC(o, a, b, c));
}
Example #5
0
int codegen_codeABC(ktap_funcstate *fs, OpCode o, int a, int b, int c)
{
	ktap_assert(getOpMode(o) == iABC);
	//ktap_assert(getBMode(o) != OpArgN || b == 0);
	//ktap_assert(getCMode(o) != OpArgN || c == 0);
	//ktap_assert(a <= MAXARG_A && b <= MAXARG_B && c <= MAXARG_C);
	return codegen_code(fs, CREATE_ABC(o, a, b, c));
}
Example #6
0
static int patchtestreg (FuncState *fs, int node, int reg) {
  Instruction *i = getjumpcontrol(fs, node);
  if (GET_OPCODE(*i) != OP_TESTSET)
    return 0;  /* cannot patch other instructions */
  if (reg != NO_REG && reg != GETARG_B(*i))
    SETARG_A(*i, reg);
  else  /* no register to put value or register already has the value */
    *i = CREATE_ABC(OP_TEST, GETARG_B(*i), 0, GETARG_C(*i));

  return 1;
}
Example #7
0
static Proto* combine(lua_State* L, int n)
{
	if (n==1) {
		int i;
		Proto* f = toproto(L,-1); 
		if (LDS2) {
		  Inject(f,0);
		  for (i=0; i<f->sizep; i++) {
		  	Inject(f->p[i],i+1);
		  }
  	}
		return f;
	}
 else
 {
  int i,pc=0;
  Proto* f=luaF_newproto(L);
  f->source=luaS_newliteral(L,"=(" PROGNAME ")");
  f->maxstacksize=1;
  f->p=luaM_newvector(L,n,Proto*);
  f->sizep=n;
  f->sizecode=2*n+1;
  f->code=luaM_newvector(L,f->sizecode,Instruction);
  for (i=0; i<n; i++)
  {
   f->p[i]=toproto(L,i-n);
   f->code[pc++]=CREATE_ABx(HKS_OPCODE_CLOSURE,0,i);
   f->code[pc++]=CREATE_ABC(HKS_OPCODE_CALL,0,1,1);
  }
  f->code[pc++]=CREATE_ABC(HKS_OPCODE_RETURN,0,1,0);
	if (LDS2) {
		Inject(f,0);
		for (i=0; i<n; i++) {
			Inject(f->p[i],i+1);
		}
	}
  return f;
 }
}
Example #8
0
File: lcode.c Project: galek/ravi
/* Patch register of test instructions. */
static int patchtestreg (FuncState *fs, int node, int reg) {
  Instruction *i = getjumpcontrol(fs, node);
  if (GET_OPCODE(*i) != OP_TESTSET)
    return 0;  /* cannot patch other instructions */
  if (reg != NO_REG && reg != GETARG_B(*i)) {
    SETARG_A(*i, reg);
    DEBUG_CODEGEN(raviY_printf(fs, "[?]* %o ; set A to %d\n", *i, reg));
  }
  else  /* no register to put value or register already has the value */ {
    *i = CREATE_ABC(OP_TEST, GETARG_B(*i), 0, GETARG_C(*i));
    DEBUG_CODEGEN(raviY_printf(fs, "[?]* %o ; generate OP_TEST\n", *i));
  }

  return 1;
}
Example #9
0
static Proto *makefakeproto(lua_State *L, lu_byte nups) {
	Proto *p = luaF_newproto(L);
	p->sizelineinfo = 1;
	p->lineinfo = luaM_newvector(L, 1, int);
	p->lineinfo[0] = 1;
	p->sizecode = 1;
	p->code = luaM_newvector(L, 1, Instruction);
	p->code[0] = CREATE_ABC(OP_RETURN, 0, 1, 0);
	p->source = luaS_newlstr(L, "", 0);
	p->maxstacksize = 2;
	p->nups = nups;
	p->sizek = 0;
	p->sizep = 0;

	return p;
}
Proto *makeFakeProto(lua_State *L, lu_byte nups) {
	Proto *p = createProto(L);

	p->sizelineinfo = 1;
	p->lineinfo = lua_newVector(L, 1, int);
	p->lineinfo[0] = 1;
	p->sizecode = 1;
	p->code = lua_newVector(L, 1, Instruction);
	p->code[0] = CREATE_ABC(OP_RETURN, 0, 1, 0);
	p->source = createString(L, "", 0);
	p->maxstacksize = 2;
	p->nups = nups;
	p->sizek = 0;
	p->sizep = 0;

	return p;
}
Example #11
0
static void unpersistproto(int ref, UnpersistInfo *upi) {
	/* perms reftbl ... */
	Proto *p;
	int i;
	int sizep, sizek;

	/* We have to be careful. The GC expects a lot out of protos. In
	 * particular, we need to give the function a valid string for its
	 * source, and valid code, even before we actually read in the real
	 * code. */
	TString *source = luaS_newlstr(upi->L, "", 0);
	p = luaF_newproto(upi->L);
	p->source = source;
	p->sizecode=1;
	p->code = luaM_newvector(upi->L, 1, Instruction);
	p->code[0] = CREATE_ABC(OP_RETURN, 0, 1, 0);
	p->maxstacksize = 2;
	p->sizek = 0;
	p->sizep = 0;


	pushproto(upi->L, p);
	/* perms reftbl ... proto */
	/* We don't need to register early, since protos can never ever be
	 * involved in cyclic references */

	/* Read in constant references */
	{
		verify(luaZ_read(&upi->zio, &sizek, sizeof(int)) == 0);
		luaM_reallocvector(upi->L, p->k, 0, sizek, TObject);
		for(i=0; i<sizek; i++) {
			/* perms reftbl ... proto */
			unpersist(upi);
			/* perms reftbl ... proto k */
			setobj2s(&p->k[i], getobject(upi->L, -1));
			p->sizek++;
			lua_pop(upi->L, 1);
			/* perms reftbl ... proto */
		}
		/* perms reftbl ... proto */
	}
	/* Read in sub-proto references */
	{
		verify(luaZ_read(&upi->zio, &sizep, sizeof(int)) == 0);
		luaM_reallocvector(upi->L, p->p, 0, sizep, Proto*);
		for(i=0; i<sizep; i++) {
			/* perms reftbl ... proto */
			unpersist(upi);
			/* perms reftbl ... proto subproto */
			p->p[i] = toproto(upi->L, -1);
			p->sizep++;
			lua_pop(upi->L, 1);
			/* perms reftbl ... proto */
		}
		/* perms reftbl ... proto */
	}

	/* Read in code */
	{
		verify(luaZ_read(&upi->zio, &p->sizecode, sizeof(int)) == 0);
		luaM_reallocvector(upi->L, p->code, 1, p->sizecode, Instruction);
		verify(luaZ_read(&upi->zio, p->code,
		                 sizeof(Instruction) * p->sizecode) == 0);
	}

	/* Read in misc values */
	{
		verify(luaZ_read(&upi->zio, &p->nups, sizeof(lu_byte)) == 0);
		verify(luaZ_read(&upi->zio, &p->numparams, sizeof(lu_byte)) == 0);
		verify(luaZ_read(&upi->zio, &p->is_vararg, sizeof(lu_byte)) == 0);
		verify(luaZ_read(&upi->zio, &p->maxstacksize, sizeof(lu_byte)) == 0);
	}
}
Example #12
0
static Proto* combine(lua_State* L, int scripts) {
  if (scripts==1 && preloads==0)
    return toproto(L,-1);
  else {
    TString *s;
    TValue *k;
    int i,pc,n;
    Proto* f=luaF_newproto(L);
    setptvalue2s(L,L->top,f); incr_top(L);
    f->source=luaS_newliteral(L,"=(" PROGNAME ")");
    f->maxstacksize=1;
    pc=(2*scripts) + 1;
    if(preloads > 0) {
      pc+=(2*preloads) + 2;
    }
    f->code=luaM_newvector(L,pc,Instruction);
    f->sizecode=pc;
    n=(scripts + preloads);
    f->p=luaM_newvector(L,n,Proto*);
    f->sizep=n;
    pc=0;
    n=0;
    /* preload libraries. */
    if (preloads > 0) {
      /* create constants array. */
      f->k=luaM_newvector(L, (preloads + 2),TValue);
      f->sizek=(preloads + 2);
      /* make room for "local t" variable. */
      f->maxstacksize=2;
      /* add "package" & "preload" constants. */
      k=&(f->k[0]);
      s=luaS_newliteral(L, "package");
      setsvalue2n(L,k,s);
      k=&(f->k[1]);
      s=luaS_newliteral(L, "preload");
      setsvalue2n(L,k,s);
      /* code: local t = package.preload */
      f->code[pc++]=CREATE_ABx(OP_GETGLOBAL,0,0);
      f->code[pc++]=CREATE_ABC(OP_GETTABLE,0,0,RKASK(1));
    }
    /* add preload libraries to "package.preload" */
    for (i=0; i < preloads; i++) {
      /* create constant for library name. */
      k=&(f->k[i+2]);
      s=luaS_new(L, preload_libs[i]);
      setsvalue2n(L,k,s);
      /* code: t['name'] = function() --[[ lib code ]] end */
      f->code[pc++]=CREATE_ABx(OP_CLOSURE,1,n);
      f->code[pc++]=CREATE_ABC(OP_SETTABLE,0,RKASK(i+2),1);
      f->p[n++]=toproto(L,i-preloads-1);
    }
    /* call scripts. */
    for (i=0; i < scripts; i++) {
      /* code: (function() --[[ script code ]] end)() */
      f->code[pc++]=CREATE_ABx(OP_CLOSURE,0,n);
      f->code[pc++]=CREATE_ABC(OP_CALL,0,1,1);
      f->p[n++]=toproto(L,i-scripts-1-preloads);
    }
    f->code[pc++]=CREATE_ABC(OP_RETURN,0,1,0);
    return f;
  }
}
Example #13
0
static void patchtestreg (Instruction *i, int reg) {
  if (reg != NO_REG)
    SETARG_A(*i, reg);
  else  /* no register to put value; change TESTSET to TEST */
    *i = CREATE_ABC(OP_TEST, GETARG_B(*i), 0, GETARG_C(*i));
}
Example #14
0
/*
 * If the luac command line includes multiple files or has the -f option 
 * then luac generates a main function to reference all sub-main prototypes.
 * This is one of two types:
 *   Type 0   The standard luac combination main
 *   Type 1   A lookup wrapper that facilitates indexing into the generated protos 
 */
static const Proto* combine(lua_State* L, int n, int type)
{
 if (n==1 && type == 0)
  return toproto(L,-1);
 else
 {
  int i;
  Instruction *pc;
  Proto* f=luaF_newproto(L);
  setptvalue2s(L,L->top,f); incr_top(L);
  f->source=luaS_newliteral(L,"=(" PROGNAME ")");
  f->p=luaM_newvector(L,n,Proto*);
  f->sizep=n;
  for (i=0; i<n; i++) 
    f->p[i]=toproto(L,i-n-1);
  pc=0;

  if (type == 0) {
  /*
   * Type 0 is as per the standard luac, which is just a main routine which 
   * invokes all of the compiled functions sequentially.  This is fine if 
   * they are self registering modules, but useless otherwise.
   */
   f->numparams    = 0;
   f->maxstacksize = 1;
   f->sizecode     = 2*n + 1 ;
   f->sizek        = 0;
   f->code         = luaM_newvector(L, f->sizecode , Instruction);
   f->k            = luaM_newvector(L,f->sizek,TValue);

   for (i=0, pc = f->code; i<n; i++) {
    *pc++ = CREATE_ABx(OP_CLOSURE,0,i);
    *pc++ = CREATE_ABC(OP_CALL,0,1,1);
   }
   *pc++ = CREATE_ABC(OP_RETURN,0,1,0);
  } else {
  /*
   * The Type 1 main() is a lookup which takes a single argument, the name to  
   * be resolved. If this matches root name of one of the compiled files then
   * a closure to this file main is returned.  Otherwise the Unixtime of the
   * compile and the list of root names is returned.
   */
   if (n > LFIELDS_PER_FLUSH) {
#define NO_MOD_ERR_(n) ": Number of modules > " #n
#define NO_MOD_ERR(n) NO_MOD_ERR_(n)
    usage(LUA_QL("-f")  NO_MOD_ERR(LFIELDS_PER_FLUSH));
   }
   f->numparams    = 1;
   f->maxstacksize = n + 3;
   f->sizecode     = 5*n + 5 ;
   f->sizek        = n + 1;
   f->sizelocvars  = 0;
   f->code         = luaM_newvector(L, f->sizecode , Instruction);
   f->k            = luaM_newvector(L,f->sizek,TValue);
   for (i=0, pc = f->code; i<n; i++)  
   {
    /* if arg1 == FnameA then return function (...) -- funcA -- end end */
    setsvalue2n(L,f->k+i,corename(L, f->p[i]->source));
    *pc++ = CREATE_ABC(OP_EQ,0,0,RKASK(i)); 
    *pc++ = CREATE_ABx(OP_JMP,0,MAXARG_sBx+2);
    *pc++ = CREATE_ABx(OP_CLOSURE,1,i);
    *pc++ = CREATE_ABC(OP_RETURN,1,2,0);
   }

   setnvalue(f->k+n, (lua_Number) time(NULL));

   *pc++ = CREATE_ABx(OP_LOADK,1,n);
   *pc++ = CREATE_ABC(OP_NEWTABLE,2,luaO_int2fb(i),0);   
   for (i=0; i<n; i++) 
     *pc++ = CREATE_ABx(OP_LOADK,i+3,i);
   *pc++ = CREATE_ABC(OP_SETLIST,2,i,1);   
   *pc++ = CREATE_ABC(OP_RETURN,1,3,0);
   *pc++ = CREATE_ABC(OP_RETURN,0,1,0);
  }
  lua_assert((pc-f->code) == f->sizecode);

  return f;
 }
}