Пример #1
0
        private static string MatchEval(Match match)
        {
            GroupCollection groupCollection = match.Groups;

            string replaceStr = "";

            replaceStr += groupCollection["whitespace"].Value; // Whitespace

            if (groupCollection["labeltype"].Value == "JUMP_LABEL,")
            {
                replaceStr += "JUMP, ";
            }
            else if (groupCollection["labeltype"].Value == "JUMP_IF_FALSE_LABEL,")
            {
                replaceStr += "JUMP_IF_FALSE, ";
            }

            string labelName = groupCollection["label"].Value;

            JumpLabel targetLabel = currentLabelTable.GetLabel(labelName);

            replaceStr += targetLabel.AddresStr();

            replaceStr += groupCollection["commentwhitespace"];

            // optional comment
            if (groupCollection.Count > 4)
            {
                replaceStr += groupCollection["comment"].Value;
            }

            return(replaceStr);
        }
Пример #2
0
        public void AddJumpLabel(JumpLabel jumpLabel, string comment = "")
        {
            if (jumpLabel.IsResolved)
            {
                throw new System.Exception($"Target jump label {jumpLabel.uniqueName} has already been used!");
            }

            jumpLabel.resolvedAddress = (uint)programCounter;
            //AppendCommentedLine("NOP", comment);
            //programCounter += UdonSharpUtils.GetUdonInstructionSize("NOP");
        }
Пример #3
0
        public void AddJumpIfFalse(JumpLabel jumpTarget, string comment = "")
        {
            if (jumpTarget.IsResolved)
            {
                AppendCommentedLine($"JUMP_IF_FALSE, {jumpTarget.AddresStr()}", comment);
            }
            else
            {
                AppendCommentedLine($"JUMP_IF_FALSE_LABEL, [{jumpTarget.uniqueName}]", comment);
            }

            programCounter += UdonSharpUtils.GetUdonInstructionSize("JUMP_IF_FALSE");
        }
        public void AddJump(JumpLabel jumpTarget, string comment = "")
        {
#if USE_UDON_LABELS
            AppendCommentedLine($"JUMP, {jumpTarget.uniqueName}", comment);
#else
            if (jumpTarget.IsResolved)
            {
                AppendCommentedLine($"JUMP, {jumpTarget.AddresStr()}", comment);
            }
            else
            {
                AppendCommentedLine($"JUMP_LABEL, [{jumpTarget.uniqueName}]", comment);
            }
#endif

            programCounter += UdonSharpUtils.GetUdonInstructionSize("JUMP");
        }
        private string ReplaceLabels(string assemblyString, LabelTable labelTable)
        {
            StringBuilder newAssemblyBuilder = new StringBuilder();

            using (StringReader reader = new StringReader(assemblyString))
            {
                string currentLine = reader.ReadLine();

                while (currentLine != null)
                {
                    string line = currentLine.TrimStart(' ', '\n', '\r');
                    if (line.StartsWith("JUMP_LABEL,"))
                    {
                        int       startIdx  = line.IndexOf('[') + 1;
                        int       endIdx    = line.IndexOf(']');
                        string    labelName = line.Substring(startIdx, endIdx - startIdx);
                        JumpLabel label     = labelTable.GetLabel(labelName);
                        newAssemblyBuilder.Append($"        JUMP, {label.AddresStr()}\n");
                    }
                    else if (line.StartsWith("JUMP_IF_FALSE_LABEL,"))
                    {
                        int       startIdx  = line.IndexOf('[') + 1;
                        int       endIdx    = line.IndexOf(']');
                        string    labelName = line.Substring(startIdx, endIdx - startIdx);
                        JumpLabel label     = labelTable.GetLabel(labelName);
                        newAssemblyBuilder.Append($"        JUMP_IF_FALSE, {label.AddresStr()}\n");
                    }
                    else
                    {
                        newAssemblyBuilder.Append(currentLine + "\n");
                    }

                    currentLine = reader.ReadLine();
                }
            }

            return(newAssemblyBuilder.ToString());
        }
Пример #6
0
        public JumpLabel GetNewJumpLabel(string labelName)
        {
            int labelCounter;

            if (!nameCounter.TryGetValue(labelName, out labelCounter))
            {
                labelCounter = 0;
                nameCounter.Add(labelName, labelCounter);
            }
            else
            {
                labelCounter = ++nameCounter[labelName];
            }

            JumpLabel newLabel = new JumpLabel();

            newLabel.originalName = labelName;
            newLabel.uniqueName   = $"{labelName}_{labelCounter}";

            jumpLabels.Add(newLabel);

            return(newLabel);
        }
 public void AddJumpIfFalse(JumpLabel jumpTarget, SymbolDefinition conditionSymbol, string comment = "")
 {
     AddPush(conditionSymbol);
     AddJumpIfFalse(jumpTarget, comment);
 }
Пример #8
0
        public SymbolDefinition Invoke(SymbolDefinition[] invokeParams)
        {
            SymbolDefinition methodResult = null;

            System.Type resultSymbolType = null;
            string      lookupSymbolName = null;

            switch (captureFunc)
            {
            case InternalFunc.TypeIDInstance:
            case InternalFunc.TypeIDGeneric:
                resultSymbolType = typeof(long);
                lookupSymbolName = "udonTypeID";
                break;

            case InternalFunc.TypeNameInstance:
            case InternalFunc.TypeNameGeneric:
                resultSymbolType = typeof(string);
                lookupSymbolName = "udonTypeName";
                break;

            default:
                throw new System.ArgumentException("Invalid internal method invocation");
            }

            methodResult = visitorContext.topTable.CreateUnnamedSymbol(resultSymbolType, SymbolDeclTypeFlags.Internal);

            if (captureFunc == InternalFunc.TypeIDInstance ||
                captureFunc == InternalFunc.TypeNameInstance)
            {
                SymbolDefinition invokeSymbol = captureScope.accessSymbol;

                using (ExpressionCaptureScope resultSetterScope = new ExpressionCaptureScope(visitorContext, null))
                {
                    resultSetterScope.SetToLocalSymbol(methodResult);

                    using (ExpressionCaptureScope getInvokeScope = new ExpressionCaptureScope(visitorContext, null))
                    {
                        getInvokeScope.SetToLocalSymbol(invokeSymbol);
                        getInvokeScope.ResolveAccessToken(nameof(VRC.Udon.UdonBehaviour.GetProgramVariable));

                        string symbolName = visitorContext.topTable.GetReflectionSymbol(lookupSymbolName, resultSymbolType).symbolUniqueName;

                        SymbolDefinition invokeResult = getInvokeScope.Invoke(new SymbolDefinition[] { visitorContext.topTable.CreateConstSymbol(typeof(string), symbolName) });

                        JumpLabel exitBranchJump = visitorContext.labelTable.GetNewJumpLabel("exitUdonTypeIdLoc");
                        JumpLabel falseBranchLoc = visitorContext.labelTable.GetNewJumpLabel("falseUdonTypeIdLoc");

                        SymbolDefinition nullCheckSymbol = null;

                        using (ExpressionCaptureScope nullCheckCondition = new ExpressionCaptureScope(visitorContext, null))
                        {
                            nullCheckCondition.SetToMethods(UdonSharpUtils.GetOperators(typeof(object), BuiltinOperatorType.Inequality));
                            nullCheckSymbol = nullCheckCondition.Invoke(new SymbolDefinition[] { invokeResult, visitorContext.topTable.CreateConstSymbol(typeof(object), null) });
                        }

                        visitorContext.uasmBuilder.AddJumpIfFalse(falseBranchLoc, nullCheckSymbol);

                        resultSetterScope.ExecuteSet(captureScope.CastSymbolToType(invokeResult, resultSymbolType, true));
                        visitorContext.uasmBuilder.AddJump(exitBranchJump);

                        // If the value is null
                        visitorContext.uasmBuilder.AddJumpLabel(falseBranchLoc);

                        if (captureFunc == InternalFunc.TypeIDInstance)
                        {
                            resultSetterScope.ExecuteSet(visitorContext.topTable.CreateConstSymbol(typeof(long), 0L));
                        }
                        else
                        {
                            resultSetterScope.ExecuteSet(visitorContext.topTable.CreateConstSymbol(typeof(string), "UnknownType"));
                        }

                        visitorContext.uasmBuilder.AddJumpLabel(exitBranchJump);
                    }
                }
            }
            else
            {
                object resultSymbolValue = null;

                if (captureFunc == InternalFunc.TypeIDGeneric)
                {
                    resultSymbolValue = Internal.UdonSharpInternalUtility.GetTypeID(genericType);
                }
                else if (captureFunc == InternalFunc.TypeNameGeneric)
                {
                    resultSymbolValue = Internal.UdonSharpInternalUtility.GetTypeName(genericType);
                }

                methodResult = visitorContext.topTable.CreateConstSymbol(resultSymbolType, resultSymbolValue);
            }

            using (ExpressionCaptureScope propagateScope = new ExpressionCaptureScope(visitorContext, visitorContext.topCaptureScope))
            {
                propagateScope.SetToLocalSymbol(methodResult);
            }

            return(methodResult);
        }