FunctionCompilationContext.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  1. using System.Collections.Concurrent;
  2. using System.Diagnostics.CodeAnalysis;
  3. using System.Runtime.CompilerServices;
  4. using Lua.Runtime;
  5. using Lua.Internal;
  6. namespace Lua.CodeAnalysis.Compilation;
  7. public class FunctionCompilationContext : IDisposable
  8. {
  9. static class Pool
  10. {
  11. static readonly ConcurrentStack<FunctionCompilationContext> stack = new();
  12. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  13. public static FunctionCompilationContext Rent()
  14. {
  15. if (!stack.TryPop(out var context))
  16. {
  17. context = new();
  18. }
  19. return context;
  20. }
  21. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  22. public static void Return(FunctionCompilationContext context)
  23. {
  24. context.Reset();
  25. stack.Push(context);
  26. }
  27. }
  28. internal static FunctionCompilationContext Create(ScopeCompilationContext? parentScope)
  29. {
  30. var context = Pool.Rent();
  31. context.ParentScope = parentScope;
  32. return context;
  33. }
  34. FunctionCompilationContext()
  35. {
  36. Scope = new()
  37. {
  38. Function = this
  39. };
  40. }
  41. // instructions
  42. FastListCore<Instruction> instructions;
  43. FastListCore<SourcePosition> instructionPositions;
  44. // constants
  45. Dictionary<LuaValue, int> constantIndexMap = new(16);
  46. FastListCore<LuaValue> constants;
  47. // functions
  48. Dictionary<ReadOnlyMemory<char>, int> functionMap = new(32, Utf16StringMemoryComparer.Default);
  49. FastListCore<Chunk> functions;
  50. // upvalues
  51. FastListCore<UpValueInfo> upvalues;
  52. FastListCore<LocalValueInfo> localVariables;
  53. // loop
  54. FastListCore<BreakDescription> breakQueue;
  55. FastListCore<GotoDescription> gotoQueue;
  56. /// <summary>
  57. /// Maximum local stack size
  58. /// </summary>
  59. public byte MaxStackPosition { get; set; }
  60. /// <summary>
  61. /// Chunk name (for debug)
  62. /// </summary>
  63. public string? ChunkName { get; set; }
  64. /// <summary>
  65. /// Level of nesting of while, repeat, and for loops
  66. /// </summary>
  67. public int LoopLevel { get; set; }
  68. /// <summary>
  69. /// Number of parameters
  70. /// </summary>
  71. public int ParameterCount { get; set; }
  72. /// <summary>
  73. /// Weather the function has variable arguments
  74. /// </summary>
  75. public bool HasVariableArguments { get; set; }
  76. /// <summary>
  77. /// Line number where the function is defined
  78. /// </summary>
  79. public int LineDefined { get; set; }
  80. /// <summary>
  81. /// Last line number where the function is defined
  82. /// </summary>
  83. public int LastLineDefined { get; set; }
  84. /// <summary>
  85. /// Parent scope context
  86. /// </summary>
  87. public ScopeCompilationContext? ParentScope { get; private set; }
  88. /// <summary>
  89. /// Top-level scope context
  90. /// </summary>
  91. public ScopeCompilationContext Scope { get; }
  92. /// <summary>
  93. /// Instructions
  94. /// </summary>
  95. public Span<Instruction> Instructions => instructions.AsSpan();
  96. /// <summary>
  97. /// Push the new instruction.
  98. /// </summary>
  99. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  100. public void PushInstruction(in Instruction instruction, in SourcePosition position)
  101. {
  102. instructions.Add(instruction);
  103. instructionPositions.Add(position);
  104. }
  105. /// <summary>
  106. /// Push or merge the new instruction.
  107. /// </summary>
  108. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  109. public void PushOrMergeInstruction(in Instruction instruction, in SourcePosition position, ref bool incrementStackPosition)
  110. {
  111. if (instructions.Length == 0)
  112. {
  113. instructions.Add(instruction);
  114. instructionPositions.Add(position);
  115. return;
  116. }
  117. var activeLocals = Scope.ActiveLocalVariables;
  118. ref var lastInstruction = ref instructions.AsSpan()[^1];
  119. var opcode = instruction.OpCode;
  120. switch (opcode)
  121. {
  122. case OpCode.Move:
  123. if (
  124. // available to merge and last A is not local variable
  125. lastInstruction.A == instruction.B && !activeLocals[lastInstruction.A])
  126. {
  127. switch (lastInstruction.OpCode)
  128. {
  129. case OpCode.LoadK:
  130. case OpCode.LoadBool when lastInstruction.C == 0:
  131. case OpCode.LoadNil when lastInstruction.B == 0:
  132. case OpCode.GetUpVal:
  133. case OpCode.GetTabUp:
  134. case OpCode.GetTable when !activeLocals[lastInstruction.B]:
  135. case OpCode.SetTabUp:
  136. case OpCode.SetUpVal:
  137. case OpCode.SetTable:
  138. case OpCode.NewTable:
  139. case OpCode.Self:
  140. case OpCode.Add:
  141. case OpCode.Sub:
  142. case OpCode.Mul:
  143. case OpCode.Div:
  144. case OpCode.Mod:
  145. case OpCode.Pow:
  146. case OpCode.Unm:
  147. case OpCode.Not:
  148. case OpCode.Len:
  149. case OpCode.Concat:
  150. {
  151. lastInstruction.A = instruction.A;
  152. incrementStackPosition = false;
  153. return;
  154. }
  155. }
  156. }
  157. break;
  158. case OpCode.GetTable:
  159. {
  160. // Merge MOVE GetTable
  161. if (lastInstruction.OpCode == OpCode.Move && !activeLocals[lastInstruction.A])
  162. {
  163. if (lastInstruction.A == instruction.B)
  164. {
  165. lastInstruction = Instruction.GetTable(instruction.A, lastInstruction.B, instruction.C);
  166. instructionPositions[^1] = position;
  167. incrementStackPosition = false;
  168. return;
  169. }
  170. }
  171. break;
  172. }
  173. case OpCode.SetTable:
  174. {
  175. // Merge MOVE SETTABLE
  176. if (lastInstruction.OpCode == OpCode.Move && !activeLocals[lastInstruction.A])
  177. {
  178. var lastB = lastInstruction.B;
  179. var lastA = lastInstruction.A;
  180. if (lastB < 255 && lastA == instruction.A)
  181. {
  182. // Merge MOVE MOVE SETTABLE
  183. if (instructions.Length > 2)
  184. {
  185. ref var last2Instruction = ref instructions.AsSpan()[^2];
  186. var last2A = last2Instruction.A;
  187. if (last2Instruction.OpCode == OpCode.Move && !activeLocals[last2A] && instruction.C == last2A)
  188. {
  189. last2Instruction = Instruction.SetTable((byte)(lastB), instruction.B, last2Instruction.B);
  190. instructions.RemoveAtSwapback(instructions.Length - 1);
  191. instructionPositions.RemoveAtSwapback(instructionPositions.Length - 1);
  192. instructionPositions[^1] = position;
  193. incrementStackPosition = false;
  194. return;
  195. }
  196. }
  197. lastInstruction = Instruction.SetTable((byte)(lastB), instruction.B, instruction.C);
  198. instructionPositions[^1] = position;
  199. incrementStackPosition = false;
  200. return;
  201. }
  202. if (lastA == instruction.C)
  203. {
  204. lastInstruction = Instruction.SetTable(instruction.A, instruction.B, lastB);
  205. instructionPositions[^1] = position;
  206. incrementStackPosition = false;
  207. return;
  208. }
  209. }
  210. else if (lastInstruction.OpCode == OpCode.GetTabUp && instructions.Length >= 2)
  211. {
  212. ref var last2Instruction = ref instructions[^2];
  213. var last2OpCode = last2Instruction.OpCode;
  214. if (last2OpCode is OpCode.LoadK or OpCode.Move)
  215. {
  216. var last2A = last2Instruction.A;
  217. if (!activeLocals[last2A] && instruction.C == last2A)
  218. {
  219. var c = last2OpCode == OpCode.LoadK ? last2Instruction.Bx + 256 : last2Instruction.B;
  220. last2Instruction = lastInstruction;
  221. lastInstruction = instruction with { C = (ushort)c };
  222. instructionPositions[^2] = instructionPositions[^1];
  223. instructionPositions[^1] = position;
  224. incrementStackPosition = false;
  225. return;
  226. }
  227. }
  228. }
  229. break;
  230. }
  231. case OpCode.Unm:
  232. case OpCode.Not:
  233. case OpCode.Len:
  234. if (lastInstruction.OpCode == OpCode.Move && !activeLocals[lastInstruction.A] && lastInstruction.A == instruction.B)
  235. {
  236. lastInstruction = instruction with { B = lastInstruction.B };
  237. instructionPositions[^1] = position;
  238. incrementStackPosition = false;
  239. return;
  240. }
  241. break;
  242. case OpCode.Return:
  243. if (lastInstruction.OpCode == OpCode.Move && instruction.B == 2 && lastInstruction.B < 256)
  244. {
  245. lastInstruction = instruction with { A = (byte)lastInstruction.B };
  246. instructionPositions[^1] = position;
  247. incrementStackPosition = false;
  248. return;
  249. }
  250. break;
  251. }
  252. instructions.Add(instruction);
  253. instructionPositions.Add(position);
  254. }
  255. /// <summary>
  256. /// Gets the index of the constant from the value, or if the constant is not registered it is added and its index is returned.
  257. /// </summary>
  258. public uint GetConstantIndex(in LuaValue value)
  259. {
  260. if (!constantIndexMap.TryGetValue(value, out var index))
  261. {
  262. index = constants.Length;
  263. constants.Add(value);
  264. constantIndexMap.Add(value, index);
  265. }
  266. return (uint)index;
  267. }
  268. public void AddOrSetFunctionProto(ReadOnlyMemory<char> name, Chunk chunk, out int index)
  269. {
  270. index = functions.Length;
  271. functionMap[name] = functions.Length;
  272. functions.Add(chunk);
  273. }
  274. public void AddFunctionProto(Chunk chunk, out int index)
  275. {
  276. index = functions.Length;
  277. functions.Add(chunk);
  278. }
  279. public bool TryGetFunctionProto(ReadOnlyMemory<char> name, [NotNullWhen(true)] out Chunk? proto)
  280. {
  281. if (functionMap.TryGetValue(name, out var index))
  282. {
  283. proto = functions[index];
  284. return true;
  285. }
  286. else
  287. {
  288. proto = null;
  289. return false;
  290. }
  291. }
  292. public void AddLocalVariable(ReadOnlyMemory<char> name, LocalVariableDescription description)
  293. {
  294. localVariables.Add(new LocalValueInfo()
  295. {
  296. Name = name,
  297. Index = description.RegisterIndex,
  298. StartPc = description.StartPc,
  299. EndPc = Instructions.Length,
  300. });
  301. }
  302. public void AddUpValue(UpValueInfo upValue)
  303. {
  304. upvalues.Add(upValue);
  305. }
  306. public bool TryGetUpValue(ReadOnlyMemory<char> name, out UpValueInfo description)
  307. {
  308. var span = upvalues.AsSpan();
  309. for (int i = 0; i < span.Length; i++)
  310. {
  311. var info = span[i];
  312. if (info.Name.Span.SequenceEqual(name.Span))
  313. {
  314. description = info;
  315. return true;
  316. }
  317. }
  318. if (ParentScope == null)
  319. {
  320. description = default;
  321. return false;
  322. }
  323. if (ParentScope.TryGetLocalVariable(name, out var localVariable))
  324. {
  325. ParentScope.HasCapturedLocalVariables = true;
  326. description = new()
  327. {
  328. Name = name,
  329. Index = localVariable.RegisterIndex,
  330. Id = upvalues.Length,
  331. IsInRegister = true,
  332. };
  333. upvalues.Add(description);
  334. return true;
  335. }
  336. else if (ParentScope.Function.TryGetUpValue(name, out var parentUpValue))
  337. {
  338. description = new()
  339. {
  340. Name = name,
  341. Index = parentUpValue.Id,
  342. Id = upvalues.Length,
  343. IsInRegister = false,
  344. };
  345. upvalues.Add(description);
  346. return true;
  347. }
  348. description = default;
  349. return false;
  350. }
  351. public void AddUnresolvedBreak(BreakDescription description, SourcePosition sourcePosition)
  352. {
  353. if (LoopLevel == 0)
  354. {
  355. LuaParseException.BreakNotInsideALoop(ChunkName, sourcePosition);
  356. }
  357. breakQueue.Add(description);
  358. }
  359. public void ResolveAllBreaks(byte startPosition, int endPosition, ScopeCompilationContext loopScope)
  360. {
  361. foreach (var description in breakQueue.AsSpan())
  362. {
  363. ref var instruction = ref Instructions[description.Index];
  364. if (loopScope.HasCapturedLocalVariables)
  365. {
  366. instruction.A = startPosition;
  367. }
  368. instruction.SBx = endPosition - description.Index;
  369. }
  370. breakQueue.Clear();
  371. }
  372. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  373. public void AddUnresolvedGoto(GotoDescription description)
  374. {
  375. gotoQueue.Add(description);
  376. }
  377. public void ResolveGoto(LabelDescription labelDescription)
  378. {
  379. for (int i = 0; i < gotoQueue.Length; i++)
  380. {
  381. var gotoDesc = gotoQueue[i];
  382. if (gotoDesc.Name.Span.SequenceEqual(labelDescription.Name.Span))
  383. {
  384. instructions[gotoDesc.JumpInstructionIndex] = Instruction.Jmp(labelDescription.RegisterIndex, labelDescription.Index - gotoDesc.JumpInstructionIndex - 1);
  385. gotoQueue.RemoveAtSwapback(i);
  386. i--;
  387. }
  388. }
  389. }
  390. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  391. public Chunk ToChunk()
  392. {
  393. // add return
  394. instructions.Add(Instruction.Return(0, 1));
  395. instructionPositions.Add( new (LastLineDefined, 0));
  396. Scope.RegisterLocalsToFunction();
  397. var locals = localVariables.AsSpan().ToArray();
  398. Array.Sort(locals, (x, y) => x.Index.CompareTo(y.Index));
  399. var chunk = new Chunk()
  400. {
  401. Name = ChunkName ?? "chunk",
  402. Instructions = instructions.AsSpan().ToArray(),
  403. SourcePositions = instructionPositions.AsSpan().ToArray(),
  404. Constants = constants.AsSpan().ToArray(),
  405. UpValues = upvalues.AsSpan().ToArray(),
  406. Locals = locals,
  407. Functions = functions.AsSpan().ToArray(),
  408. ParameterCount = ParameterCount,
  409. HasVariableArguments = HasVariableArguments,
  410. MaxStackPosition = MaxStackPosition,
  411. LineDefined = LineDefined,
  412. LastLineDefined = LastLineDefined,
  413. };
  414. foreach (var function in functions.AsSpan())
  415. {
  416. function.Parent = chunk;
  417. }
  418. return chunk;
  419. }
  420. /// <summary>
  421. /// Resets the values ​​held in the context.
  422. /// </summary>
  423. public void Reset()
  424. {
  425. Scope.Reset();
  426. instructions.Clear();
  427. instructionPositions.Clear();
  428. constantIndexMap.Clear();
  429. constants.Clear();
  430. upvalues.Clear();
  431. localVariables.Clear();
  432. functionMap.Clear();
  433. functions.Clear();
  434. breakQueue.Clear();
  435. gotoQueue.Clear();
  436. ChunkName = null;
  437. LoopLevel = 0;
  438. ParameterCount = 0;
  439. HasVariableArguments = false;
  440. MaxStackPosition = 0;
  441. }
  442. /// <summary>
  443. /// Returns the context object to the pool.
  444. /// </summary>
  445. public void Dispose()
  446. {
  447. ParentScope = null;
  448. Pool.Return(this);
  449. }
  450. }