NullableType.java 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. package com.oblivm.backend.lang.inter;
  2. import com.oblivm.backend.circuits.arithmetic.IntegerLib;
  3. import com.oblivm.backend.flexsc.CompEnv;
  4. import com.oblivm.backend.flexsc.IWritable;
  5. import com.oblivm.backend.flexsc.Mode;
  6. public class NullableType<T1, T2 extends IWritable<T2, T1>> implements IWritable<NullableType<T1, T2>, T1> {
  7. public T1 isNull;
  8. public T2 value;
  9. public CompEnv<T1> env;
  10. public IntegerLib<T1> intLib;
  11. public NullableType(CompEnv<T1> env, T2 data, T1 isNull) {
  12. this.value = data;
  13. this.isNull = isNull;
  14. this.env = env;
  15. this.intLib = new IntegerLib<T1>(env);
  16. }
  17. @Override
  18. public int numBits() {
  19. return value.numBits() + 1;
  20. }
  21. @Override
  22. public T1[] getBits() throws Exception {
  23. T1[] ret = env.newTArray(numBits());
  24. ret[0] = isNull;
  25. System.arraycopy(value.getBits(), 0, ret, 1, ret.length - 1);
  26. return ret;
  27. }
  28. @Override
  29. public NullableType<T1, T2> newObj(T1[] data) throws Exception {
  30. T1[] ret = env.newTArray(data.length - 1);
  31. System.arraycopy(data, 1, ret, 0, ret.length);
  32. return new NullableType<T1, T2>(env, value.newObj(ret), data[0]);
  33. }
  34. public NullableType<T1, T2> mux(T1 is, NullableType<T1, T2> obj) throws Exception {
  35. if (env.mode == Mode.COUNT) {
  36. intLib.mux(value.getBits(), obj.value.getBits(), is);
  37. intLib.mux(isNull, obj.isNull, is);
  38. return this;
  39. }
  40. return new NullableType<T1, T2>(env, value.newObj(intLib.mux(value.getBits(), obj.getBits(), is)),
  41. intLib.mux(isNull, obj.isNull, is));
  42. }
  43. public T2 muxFake() throws Exception {
  44. if (env.mode == Mode.COUNT) {
  45. intLib.mux(value.getBits(), getFake().getBits(), isNull);
  46. return value;
  47. }
  48. return value.newObj(intLib.mux(value.getBits(), getFake().getBits(), isNull));
  49. }
  50. public T2 getFake() throws Exception {
  51. if (env.mode == Mode.COUNT) {
  52. return value;
  53. }
  54. return value.fake();
  55. }
  56. }