Change ByteCode and ByteCodeFunction to classes

The lack of encapsulation was finally starting to bother me. Had to
change the Interpreter namespace to a struct so that it could be
friended, but otherwise this was a nice and simple cleanup.

Also updated the comments on the two run functions, and renamed
fInputSlots to fUniformSlots, to reflect recent clarification
around in vs. uniform.

Change-Id: I24bbc59778b3ab6448bffcf98133d5c149a060a9
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/244883
Reviewed-by: Ethan Nicholas <ethannicholas@google.com>
Commit-Queue: Brian Osman <brianosman@google.com>
diff --git a/src/sksl/SkSLByteCode.cpp b/src/sksl/SkSLByteCode.cpp
index bb8f0c6..1f0841f 100644
--- a/src/sksl/SkSLByteCode.cpp
+++ b/src/sksl/SkSLByteCode.cpp
@@ -20,10 +20,10 @@
 
 #if defined(SK_ENABLE_SKSL_INTERPRETER)
 
-namespace Interpreter {
-
 constexpr int VecWidth = ByteCode::kVecWidth;
 
+struct Interpreter {
+
 using F32 = skvx::Vec<VecWidth, float>;
 using I32 = skvx::Vec<VecWidth, int32_t>;
 using U32 = skvx::Vec<VecWidth, uint32_t>;
@@ -54,7 +54,7 @@
     VECTOR_DISASSEMBLE_NO_COUNT(op, text)            \
     case ByteCodeInstruction::op##N: printf(text "N %d", READ8()); break;
 
-static const uint8_t* disassemble_instruction(const uint8_t* ip) {
+static const uint8_t* DisassembleInstruction(const uint8_t* ip) {
     switch ((ByteCodeInstruction) (intptr_t) READ_INST()) {
         VECTOR_MATRIX_DISASSEMBLE(kAddF, "addf")
         VECTOR_DISASSEMBLE(kAddI, "addi")
@@ -405,18 +405,9 @@
 
 union VValue {
     VValue() {}
-
-    VValue(F32 f)
-        : fFloat(f) {
-    }
-
-    VValue(I32 s)
-        : fSigned(s) {
-    }
-
-    VValue(U32 u)
-        : fUnsigned(u) {
-    }
+    VValue(F32 f) : fFloat(f) {}
+    VValue(I32 s) : fSigned(s) {}
+    VValue(U32 u) : fUnsigned(u) {}
 
     F32 fFloat;
     I32 fSigned;
@@ -430,14 +421,13 @@
     int fParameterCount;
 };
 
-template <typename T>
-static T vec_mod(T a, T b) {
+static F32 VecMod(F32 a, F32 b) {
     return a - skvx::trunc(a / b) * b;
 }
 
 #define spf(index)  sp[index].fFloat
 
-static void call_external(const ByteCode* byteCode, const uint8_t*& ip, VValue*& sp,
+static void CallExternal(const ByteCode* byteCode, const uint8_t*& ip, VValue*& sp,
                           int baseIndex, I32 mask) {
     int argumentCount = READ8();
     int returnCount = READ8();
@@ -464,7 +454,7 @@
     sp += returnCount - 1;
 }
 
-static void inverse2x2(VValue* sp) {
+static void Inverse2x2(VValue* sp) {
     F32 a = sp[-3].fFloat,
         b = sp[-2].fFloat,
         c = sp[-1].fFloat,
@@ -476,7 +466,7 @@
     sp[ 0].fFloat = a * idet;
 }
 
-static void inverse3x3(VValue* sp) {
+static void Inverse3x3(VValue* sp) {
     F32 a11 = sp[-8].fFloat, a12 = sp[-5].fFloat, a13 = sp[-2].fFloat,
         a21 = sp[-7].fFloat, a22 = sp[-4].fFloat, a23 = sp[-1].fFloat,
         a31 = sp[-6].fFloat, a32 = sp[-3].fFloat, a33 = sp[ 0].fFloat;
@@ -493,7 +483,7 @@
     sp[ 0].fFloat = (a11 * a22 - a12 * a21) * idet;
 }
 
-static void inverse4x4(VValue* sp) {
+static void Inverse4x4(VValue* sp) {
     F32 a00 = spf(-15), a10 = spf(-11), a20 = spf( -7), a30 = spf( -3),
         a01 = spf(-14), a11 = spf(-10), a21 = spf( -6), a31 = spf( -2),
         a02 = spf(-13), a12 = spf( -9), a22 = spf( -5), a32 = spf( -1),
@@ -546,7 +536,7 @@
     spf(  0) = a20 * b03 - a21 * b01 + a22 * b00;
 }
 
-static bool innerRun(const ByteCode* byteCode, const ByteCodeFunction* f, VValue* stack,
+static bool InnerRun(const ByteCode* byteCode, const ByteCodeFunction* f, VValue* stack,
                      float* outReturn[], VValue globals[], bool stripedOutput, int N,
                      int baseIndex) {
 #ifdef SKSLC_THREADED_CODE
@@ -828,7 +818,7 @@
     }
 
     LABEL(kCallExternal) {
-        call_external(byteCode, ip, sp, baseIndex, mask());
+        CallExternal(byteCode, ip, sp, baseIndex, mask());
         NEXT();
     }
 
@@ -896,15 +886,15 @@
     }
 
     LABEL(kInverse2x2) {
-        inverse2x2(sp);
+        Inverse2x2(sp);
         NEXT();
     }
     LABEL(kInverse3x3) {
-        inverse3x3(sp);
+        Inverse3x3(sp);
         NEXT();
     }
     LABEL(kInverse4x4) {
-        inverse4x4(sp);
+        Inverse4x4(sp);
         NEXT();
     }
 
@@ -1077,7 +1067,7 @@
         NEXT();
     }
 
-    VECTOR_BINARY_FN(kRemainderF, fFloat, vec_mod<F32>)
+    VECTOR_BINARY_FN(kRemainderF, fFloat, VecMod)
     VECTOR_BINARY_MASKED_OP(kRemainderS, fSigned, %)
     VECTOR_BINARY_MASKED_OP(kRemainderU, fUnsigned, %)
 
@@ -1361,7 +1351,7 @@
 #endif
 }
 
-} // namespace Interpreter
+}; // class Interpreter
 
 #endif // SK_ENABLE_SKSL_INTERPRETER
 
@@ -1372,7 +1362,7 @@
     const uint8_t* ip = fCode.data();
     while (ip < fCode.data() + fCode.size()) {
         printf("%d: ", (int)(ip - fCode.data()));
-        ip = Interpreter::disassemble_instruction(ip);
+        ip = Interpreter::DisassembleInstruction(ip);
         printf("\n");
     }
 #endif
@@ -1587,7 +1577,7 @@
 
     if (argCount != f->fParameterCount ||
         returnCount != f->fReturnCount ||
-        uniformCount != (int)fInputSlots.size()) {
+        uniformCount != (int)fUniformSlots.size()) {
         return false;
     }
 
@@ -1595,7 +1585,7 @@
     if (fGlobalCount > (int)SK_ARRAY_COUNT(globals)) {
         return false;
     }
-    for (uint8_t slot : fInputSlots) {
+    for (uint8_t slot : fUniformSlots) {
         globals[slot].fFloat = *uniforms++;
     }
 
@@ -1605,13 +1595,13 @@
         float* dst = (float*)stack;
         for (int i = 0; i < argCount; ++i) {
             *dst = *src++;
-            dst += Interpreter::VecWidth;
+            dst += VecWidth;
         }
     }
 
     bool stripedOutput = false;
     float** outArray = outReturn ? &outReturn : nullptr;
-    if (!innerRun(this, f, stack, outArray, globals, stripedOutput, 1, 0)) {
+    if (!Interpreter::InnerRun(this, f, stack, outArray, globals, stripedOutput, 1, 0)) {
         return false;
     }
 
@@ -1623,11 +1613,11 @@
             if (p.fIsOutParameter) {
                 for (int i = p.fSlotCount; i > 0; --i) {
                     *dst++ = *src;
-                    src += Interpreter::VecWidth;
+                    src += VecWidth;
                 }
             } else {
                 dst += p.fSlotCount;
-                src += p.fSlotCount * Interpreter::VecWidth;
+                src += p.fSlotCount * VecWidth;
             }
         }
     }
@@ -1652,7 +1642,7 @@
 
     if (argCount != f->fParameterCount ||
         returnCount != f->fReturnCount ||
-        uniformCount != (int)fInputSlots.size()) {
+        uniformCount != (int)fUniformSlots.size()) {
         return false;
     }
 
@@ -1660,7 +1650,7 @@
     if (fGlobalCount > (int)SK_ARRAY_COUNT(globals)) {
         return false;
     }
-    for (uint8_t slot : fInputSlots) {
+    for (uint8_t slot : fUniformSlots) {
         globals[slot].fFloat = *uniforms++;
     }
 
@@ -1672,7 +1662,7 @@
     int baseIndex = 0;
 
     while (N) {
-        int w = std::min(N, Interpreter::VecWidth);
+        int w = std::min(N, VecWidth);
 
         // Copy args into stack
         for (int i = 0; i < argCount; ++i) {
@@ -1680,7 +1670,8 @@
         }
 
         bool stripedOutput = true;
-        if (!innerRun(this, f, stack, outReturn, globals, stripedOutput, w, baseIndex)) {
+        if (!Interpreter::InnerRun(this, f, stack, outReturn, globals, stripedOutput, w,
+                                   baseIndex)) {
             return false;
         }