opcodeFrom static method

List<int> opcodeFrom(
  1. EvcOp op
)

Write an EvcOp bytecode to a list of bytes.

Implementation

static List<int> opcodeFrom(EvcOp op) {
  switch (op.runtimeType) {
    case JumpConstant:
      op as JumpConstant;
      return [Evc.OP_JMPC, ...Evc.i32b(op._offset)];
    case Exit:
      op as Exit;
      return [Evc.OP_EXIT, ...Evc.i16b(op._location)];
    case Unbox:
      op as Unbox;
      return [Evc.OP_UNBOX, ...Evc.i16b(op._reg)];
    case PushReturnValue:
      op as PushReturnValue;
      return [Evc.OP_SETVR];
    case NumAdd:
      op as NumAdd;
      return [
        Evc.OP_ADDVV,
        ...Evc.i16b(op._location1),
        ...Evc.i16b(op._location2)
      ];
    case NumSub:
      op as NumSub;
      return [
        Evc.OP_NUM_SUB,
        ...Evc.i16b(op._location1),
        ...Evc.i16b(op._location2)
      ];
    case BoxInt:
      op as BoxInt;
      return [Evc.OP_BOXINT, ...Evc.i16b(op._reg)];
    case BoxDouble:
      op as BoxDouble;
      return [Evc.OP_BOXDOUBLE, ...Evc.i16b(op._reg)];
    case BoxNum:
      op as BoxNum;
      return [Evc.OP_BOXNUM, ...Evc.i16b(op._reg)];
    case PushArg:
      op as PushArg;
      return [Evc.OP_PUSH_ARG, ...Evc.i16b(op._location)];
    case JumpIfNonNull:
      op as JumpIfNonNull;
      return [Evc.OP_JNZ, ...Evc.i16b(op._location), ...Evc.i32b(op._offset)];
    case JumpIfFalse:
      op as JumpIfFalse;
      return [
        Evc.OP_JUMP_IF_FALSE,
        ...Evc.i16b(op._location),
        ...Evc.i32b(op._offset)
      ];
    case PushConstantInt:
      op as PushConstantInt;
      return [Evc.OP_SETVC, ...Evc.i32b(op._value)];
    case PushScope:
      op as PushScope;
      return [
        Evc.OP_PUSHSCOPE,
        ...Evc.i32b(op.sourceFile),
        ...Evc.i32b(op.sourceOffset),
        ...Evc.istr(op.frName)
      ];
    case PopScope:
      op as PopScope;
      return [Evc.OP_POPSCOPE];
    case CopyValue:
      op as CopyValue;
      return [Evc.OP_SETVV, ...Evc.i16b(op._to), ...Evc.i16b(op._from)];
    case SetReturnValue:
      op as SetReturnValue;
      return [Evc.OP_SETRV, ...Evc.i16b(op._location)];
    case Return:
      op as Return;
      return [Evc.OP_RETURN, ...Evc.i16b(op._location)];
    case ReturnAsync:
      op as ReturnAsync;
      return [
        Evc.OP_RETURN_ASYNC,
        ...Evc.i16b(op._location),
        ...Evc.i16b(op._completerOffset)
      ];
    case Pop:
      op as Pop;
      return [Evc.OP_POP, op._amount];
    case Call:
      op as Call;
      return [Evc.OP_CALL, ...Evc.i32b(op._offset)];
    case InvokeDynamic:
      op as InvokeDynamic;
      return [
        Evc.OP_INVOKE_DYNAMIC,
        ...Evc.i16b(op._location),
        ...Evc.i32b(op._methodIdx)
      ];
    case SetObjectProperty:
      op as SetObjectProperty;
      return [
        Evc.OP_SET_OBJECT_PROP,
        ...Evc.i16b(op._location),
        ...Evc.istr(op._property),
        ...Evc.i16b(op._valueOffset)
      ];
    case PushObjectProperty:
      op as PushObjectProperty;
      return [
        Evc.OP_PUSH_OBJECT_PROP,
        ...Evc.i16b(op._location),
        ...Evc.i32b(op._propertyIdx)
      ];
    case PushObjectPropertyImpl:
      op as PushObjectPropertyImpl;
      return [
        Evc.OP_PUSH_OBJECT_PROP_IMPL,
        ...Evc.i16b(op.objectOffset),
        ...Evc.i16b(op._propertyIndex)
      ];
    case SetObjectPropertyImpl:
      op as SetObjectPropertyImpl;
      return [
        Evc.OP_SET_OBJECT_PROP_IMPL,
        ...Evc.i16b(op._objectOffset),
        ...Evc.i16b(op._propertyIndex),
        ...Evc.i16b(op._valueOffset)
      ];
    case PushNull:
      op as PushNull;
      return [Evc.OP_PUSH_NULL];
    case CreateClass:
      op as CreateClass;
      return [
        Evc.OP_CREATE_CLASS,
        ...Evc.i32b(op._library),
        ...Evc.i16b(op._super),
        ...Evc.istr(op._name),
        ...Evc.i16b(op._valuesLen)
      ];

    case NumLt:
      op as NumLt;
      return [
        Evc.OP_NUM_LT,
        ...Evc.i16b(op._location1),
        ...Evc.i16b(op._location2)
      ];
    case NumLtEq:
      op as NumLtEq;
      return [
        Evc.OP_NUM_LT_EQ,
        ...Evc.i16b(op._location1),
        ...Evc.i16b(op._location2)
      ];
    case PushSuper:
      op as PushSuper;
      return [Evc.OP_PUSH_SUPER, ...Evc.i16b(op._objectOffset)];
    case BridgeInstantiate:
      op as BridgeInstantiate;
      return [
        Evc.OP_BRIDGE_INSTANTIATE,
        ...Evc.i16b(op._subclass),
        ...Evc.i32b(op._constructor)
      ];
    case PushBridgeSuperShim:
      op as PushBridgeSuperShim;
      return [Evc.OP_PUSH_SUPER_SHIM];
    case ParentBridgeSuperShim:
      op as ParentBridgeSuperShim;
      return [
        Evc.OP_PARENT_SUPER_SHIM,
        ...Evc.i16b(op._shimOffset),
        ...Evc.i16b(op._bridgeOffset)
      ];
    case PushList:
      op as PushList;
      return [Evc.OP_PUSH_LIST];
    case ListAppend:
      op as ListAppend;
      return [
        Evc.OP_LIST_APPEND,
        ...Evc.i16b(op._reg),
        ...Evc.i16b(op._value)
      ];
    case IndexList:
      op as IndexList;
      return [
        Evc.OP_INDEX_LIST,
        ...Evc.i16b(op._position),
        ...Evc.i32b(op._index)
      ];
    case PushIterableLength:
      op as PushIterableLength;
      return [Evc.OP_ITER_LENGTH, ...Evc.i16b(op._position)];
    case ListSetIndexed:
      op as ListSetIndexed;
      return [
        Evc.OP_LIST_SETINDEXED,
        ...Evc.i16b(op._position),
        ...Evc.i32b(op._index),
        ...Evc.i16b(op._value)
      ];
    case BoxString:
      op as BoxString;
      return [Evc.OP_BOXSTRING, ...Evc.i16b(op._reg)];
    case BoxList:
      op as BoxList;
      return [Evc.OP_BOXLIST, ...Evc.i16b(op._reg)];
    case BoxMap:
      op as BoxMap;
      return [Evc.OP_BOXMAP, ...Evc.i16b(op._reg)];
    case BoxBool:
      op as BoxBool;
      return [Evc.OP_BOXBOOL, ...Evc.i16b(op._reg)];
    case BoxNull:
      op as BoxNull;
      return [Evc.OP_BOX_NULL, ...Evc.i16b(op._reg)];
    case PushCaptureScope:
      op as PushCaptureScope;
      return [Evc.OP_CAPTURE_SCOPE];
    case PushConstant:
      op as PushConstant;
      return [Evc.OP_PUSH_CONST, ...Evc.i32b(op._const)];
    case PushFunctionPtr:
      op as PushFunctionPtr;
      return [Evc.OP_PUSH_FUNCTION_PTR, ...Evc.i32b(op._offset)];
    case InvokeExternal:
      op as InvokeExternal;
      return [Evc.OP_INVOKE_EXTERNAL, ...Evc.i32b(op._function)];
    case Await:
      op as Await;
      return [
        Evc.OP_AWAIT,
        ...Evc.i16b(op._completerOffset),
        ...Evc.i16b(op._futureOffset)
      ];
    case PushMap:
      op as PushMap;
      return [Evc.OP_PUSH_MAP];
    case MapSet:
      op as MapSet;
      return [
        Evc.OP_MAP_SET,
        ...Evc.i16b(op._map),
        ...Evc.i16b(op._index),
        ...Evc.i16b(op._value)
      ];
    case IndexMap:
      op as IndexMap;
      return [Evc.OP_INDEX_MAP, ...Evc.i16b(op._map), ...Evc.i16b(op._index)];
    case PushConstantDouble:
      op as PushConstantDouble;
      return [Evc.OP_PUSH_DOUBLE, ...Evc.f32b(op._value)];
    case SetGlobal:
      op as SetGlobal;
      return [
        Evc.OP_SET_GLOBAL,
        ...Evc.i32b(op._index),
        ...Evc.i16b(op._value)
      ];
    case LoadGlobal:
      op as LoadGlobal;
      return [Evc.OP_LOAD_GLOBAL, ...Evc.i32b(op._index)];
    case PushTrue:
      op as PushTrue;
      return [Evc.OP_PUSH_TRUE];
    case LogicalNot:
      op as LogicalNot;
      return [Evc.OP_LOGICAL_NOT, ...Evc.i16b(op._index)];
    case CheckEq:
      op as CheckEq;
      return [
        Evc.OP_CHECK_EQ,
        ...Evc.i16b(op._value1),
        ...Evc.i16b(op._value2)
      ];
    case Try:
      op as Try;
      return [Evc.OP_TRY, ...Evc.i32b(op._catchOffset)];
    case Throw:
      op as Throw;
      return [Evc.OP_THROW, ...Evc.i16b(op._location)];
    case PopCatch:
      op as PopCatch;
      return [Evc.OP_POP_CATCH];
    case IsType:
      op as IsType;
      return [
        Evc.OP_IS_TYPE,
        ...Evc.i16b(op._objectOffset),
        ...Evc.i32b(op._type),
        op._not ? 1 : 0
      ];
    case Assert:
      op as Assert;
      return [
        Evc.OP_ASSERT,
        ...Evc.i16b(op._valueOffset),
        ...Evc.i16b(op._exceptionOffset)
      ];
    case PushFinally:
      op as PushFinally;
      return [Evc.OP_PUSH_FINALLY, ...Evc.i32b(op._tryOffset)];
    case PushReturnFromCatch:
      op as PushReturnFromCatch;
      return [Evc.OP_PUSH_RETURN_FROM_CATCH];
    case MaybeBoxNull:
      op as MaybeBoxNull;
      return [Evc.OP_MAYBE_BOX_NULL, ...Evc.i16b(op._reg)];
    case PushRuntimeType:
      op as PushRuntimeType;
      return [Evc.OP_PUSH_RUNTIME_TYPE, ...Evc.i16b(op._value)];
    case PushConstantType:
      op as PushConstantType;
      return [Evc.OP_PUSH_CONSTANT_TYPE, ...Evc.i32b(op._typeId)];
    default:
      throw ArgumentError('Not a valid op $op');
  }
}