virtual_machine.odin 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646
  1. package regex_vm
  2. /*
  3. (c) Copyright 2024 Feoramund <[email protected]>.
  4. Made available under Odin's BSD-3 license.
  5. List of contributors:
  6. Feoramund: Initial implementation.
  7. */
  8. import "base:intrinsics"
  9. @require import "core:io"
  10. import "core:slice"
  11. import "core:text/regex/common"
  12. import "core:text/regex/parser"
  13. import "core:unicode/utf8"
  14. Rune_Class_Range :: parser.Rune_Class_Range
  15. // NOTE: This structure differs intentionally from the one in `regex/parser`,
  16. // as this data doesn't need to be a dynamic array once it hits the VM.
  17. Rune_Class_Data :: struct {
  18. runes: []rune,
  19. ranges: []Rune_Class_Range,
  20. }
  21. Opcode :: enum u8 {
  22. // | [ operands ]
  23. Match = 0x00, // |
  24. Match_And_Exit = 0x01, // |
  25. Byte = 0x02, // | u8
  26. Rune = 0x03, // | i32
  27. Rune_Class = 0x04, // | u8
  28. Rune_Class_Negated = 0x05, // | u8
  29. Wildcard = 0x06, // |
  30. Jump = 0x07, // | u16
  31. Split = 0x08, // | u16, u16
  32. Save = 0x09, // | u8
  33. Assert_Start = 0x0A, // |
  34. Assert_End = 0x0B, // |
  35. Assert_Word_Boundary = 0x0C, // |
  36. Assert_Non_Word_Boundary = 0x0D, // |
  37. Multiline_Open = 0x0E, // |
  38. Multiline_Close = 0x0F, // |
  39. Wait_For_Byte = 0x10, // | u8
  40. Wait_For_Rune = 0x11, // | i32
  41. Wait_For_Rune_Class = 0x12, // | u8
  42. Wait_For_Rune_Class_Negated = 0x13, // | u8
  43. Match_All_And_Escape = 0x14, // |
  44. }
  45. Thread :: struct {
  46. pc: int,
  47. saved: ^[2 * common.MAX_CAPTURE_GROUPS]int,
  48. }
  49. Program :: []Opcode
  50. Machine :: struct {
  51. // Program state
  52. memory: string,
  53. class_data: []Rune_Class_Data,
  54. code: Program,
  55. // Thread state
  56. top_thread: int,
  57. threads: [^]Thread,
  58. next_threads: [^]Thread,
  59. // The busy map is used to merge threads based on their program counters.
  60. busy_map: []u64,
  61. // Global state
  62. string_pointer: int,
  63. current_rune: rune,
  64. current_rune_size: int,
  65. next_rune: rune,
  66. next_rune_size: int,
  67. }
  68. // @MetaCharacter
  69. // NOTE: This must be kept in sync with the compiler & tokenizer.
  70. is_word_class :: #force_inline proc "contextless" (r: rune) -> bool {
  71. switch r {
  72. case '0'..='9', 'A'..='Z', '_', 'a'..='z':
  73. return true
  74. case:
  75. return false
  76. }
  77. }
  78. set_busy_map :: #force_inline proc "contextless" (vm: ^Machine, pc: int) -> bool #no_bounds_check {
  79. slot := cast(u64)pc >> 6
  80. bit: u64 = 1 << (cast(u64)pc & 0x3F)
  81. if vm.busy_map[slot] & bit > 0 {
  82. return false
  83. }
  84. vm.busy_map[slot] |= bit
  85. return true
  86. }
  87. check_busy_map :: #force_inline proc "contextless" (vm: ^Machine, pc: int) -> bool #no_bounds_check {
  88. slot := cast(u64)pc >> 6
  89. bit: u64 = 1 << (cast(u64)pc & 0x3F)
  90. return vm.busy_map[slot] & bit > 0
  91. }
  92. add_thread :: proc(vm: ^Machine, saved: ^[2 * common.MAX_CAPTURE_GROUPS]int, pc: int) #no_bounds_check {
  93. if check_busy_map(vm, pc) {
  94. return
  95. }
  96. saved := saved
  97. pc := pc
  98. resolution_loop: for {
  99. if !set_busy_map(vm, pc) {
  100. return
  101. }
  102. when common.ODIN_DEBUG_REGEX {
  103. io.write_string(common.debug_stream, "Thread [PC:")
  104. common.write_padded_hex(common.debug_stream, pc, 4)
  105. io.write_string(common.debug_stream, "] thinking about ")
  106. io.write_string(common.debug_stream, opcode_to_name(vm.code[pc]))
  107. io.write_rune(common.debug_stream, '\n')
  108. }
  109. #partial switch vm.code[pc] {
  110. case .Jump:
  111. pc = cast(int)intrinsics.unaligned_load(cast(^u16)&vm.code[pc + size_of(Opcode)])
  112. continue
  113. case .Split:
  114. jmp_x := cast(int)intrinsics.unaligned_load(cast(^u16)&vm.code[pc + size_of(Opcode)])
  115. jmp_y := cast(int)intrinsics.unaligned_load(cast(^u16)&vm.code[pc + size_of(Opcode) + size_of(u16)])
  116. add_thread(vm, saved, jmp_x)
  117. pc = jmp_y
  118. continue
  119. case .Save:
  120. new_saved := new([2 * common.MAX_CAPTURE_GROUPS]int)
  121. new_saved ^= saved^
  122. saved = new_saved
  123. index := vm.code[pc + size_of(Opcode)]
  124. sp := vm.string_pointer+vm.current_rune_size
  125. saved[index] = sp
  126. when common.ODIN_DEBUG_REGEX {
  127. io.write_string(common.debug_stream, "Thread [PC:")
  128. common.write_padded_hex(common.debug_stream, pc, 4)
  129. io.write_string(common.debug_stream, "] saving state: (slot ")
  130. io.write_int(common.debug_stream, cast(int)index)
  131. io.write_string(common.debug_stream, " = ")
  132. io.write_int(common.debug_stream, sp)
  133. io.write_string(common.debug_stream, ")\n")
  134. }
  135. pc += size_of(Opcode) + size_of(u8)
  136. continue
  137. case .Assert_Start:
  138. sp := vm.string_pointer+vm.current_rune_size
  139. if sp == 0 {
  140. pc += size_of(Opcode)
  141. continue
  142. }
  143. case .Assert_End:
  144. sp := vm.string_pointer+vm.current_rune_size
  145. if sp == len(vm.memory) {
  146. pc += size_of(Opcode)
  147. continue
  148. }
  149. case .Multiline_Open:
  150. sp := vm.string_pointer+vm.current_rune_size
  151. if sp == 0 || sp == len(vm.memory) {
  152. if vm.next_rune == '\r' || vm.next_rune == '\n' {
  153. // The VM is currently on a newline at the string boundary,
  154. // so consume the newline next frame.
  155. when common.ODIN_DEBUG_REGEX {
  156. io.write_string(common.debug_stream, "*** New thread added [PC:")
  157. common.write_padded_hex(common.debug_stream, pc, 4)
  158. io.write_string(common.debug_stream, "]\n")
  159. }
  160. vm.next_threads[vm.top_thread] = Thread{ pc = pc, saved = saved }
  161. vm.top_thread += 1
  162. } else {
  163. // Skip the `Multiline_Close` opcode.
  164. pc += 2 * size_of(Opcode)
  165. continue
  166. }
  167. } else {
  168. // Not on a string boundary.
  169. // Try to consume a newline next frame in the other opcode loop.
  170. when common.ODIN_DEBUG_REGEX {
  171. io.write_string(common.debug_stream, "*** New thread added [PC:")
  172. common.write_padded_hex(common.debug_stream, pc, 4)
  173. io.write_string(common.debug_stream, "]\n")
  174. }
  175. vm.next_threads[vm.top_thread] = Thread{ pc = pc, saved = saved }
  176. vm.top_thread += 1
  177. }
  178. case .Assert_Word_Boundary:
  179. sp := vm.string_pointer+vm.current_rune_size
  180. if sp == 0 || sp == len(vm.memory) {
  181. pc += size_of(Opcode)
  182. continue
  183. } else {
  184. last_rune_is_wc := is_word_class(vm.current_rune)
  185. this_rune_is_wc := is_word_class(vm.next_rune)
  186. if last_rune_is_wc && !this_rune_is_wc || !last_rune_is_wc && this_rune_is_wc {
  187. pc += size_of(Opcode)
  188. continue
  189. }
  190. }
  191. case .Assert_Non_Word_Boundary:
  192. sp := vm.string_pointer+vm.current_rune_size
  193. if sp != 0 && sp != len(vm.memory) {
  194. last_rune_is_wc := is_word_class(vm.current_rune)
  195. this_rune_is_wc := is_word_class(vm.next_rune)
  196. if last_rune_is_wc && this_rune_is_wc || !last_rune_is_wc && !this_rune_is_wc {
  197. pc += size_of(Opcode)
  198. continue
  199. }
  200. }
  201. case .Wait_For_Byte:
  202. operand := cast(rune)vm.code[pc + size_of(Opcode)]
  203. if vm.next_rune == operand {
  204. add_thread(vm, saved, pc + size_of(Opcode) + size_of(u8))
  205. }
  206. when common.ODIN_DEBUG_REGEX {
  207. io.write_string(common.debug_stream, "*** New thread added [PC:")
  208. common.write_padded_hex(common.debug_stream, pc, 4)
  209. io.write_string(common.debug_stream, "]\n")
  210. }
  211. vm.next_threads[vm.top_thread] = Thread{ pc = pc, saved = saved }
  212. vm.top_thread += 1
  213. case .Wait_For_Rune:
  214. operand := intrinsics.unaligned_load(cast(^rune)&vm.code[pc + size_of(Opcode)])
  215. if vm.next_rune == operand {
  216. add_thread(vm, saved, pc + size_of(Opcode) + size_of(rune))
  217. }
  218. when common.ODIN_DEBUG_REGEX {
  219. io.write_string(common.debug_stream, "*** New thread added [PC:")
  220. common.write_padded_hex(common.debug_stream, pc, 4)
  221. io.write_string(common.debug_stream, "]\n")
  222. }
  223. vm.next_threads[vm.top_thread] = Thread{ pc = pc, saved = saved }
  224. vm.top_thread += 1
  225. case .Wait_For_Rune_Class:
  226. operand := cast(u8)vm.code[pc + size_of(Opcode)]
  227. class_data := vm.class_data[operand]
  228. next_rune := vm.next_rune
  229. check: {
  230. for r in class_data.runes {
  231. if next_rune == r {
  232. add_thread(vm, saved, pc + size_of(Opcode) + size_of(u8))
  233. break check
  234. }
  235. }
  236. for range in class_data.ranges {
  237. if range.lower <= next_rune && next_rune <= range.upper {
  238. add_thread(vm, saved, pc + size_of(Opcode) + size_of(u8))
  239. break check
  240. }
  241. }
  242. }
  243. when common.ODIN_DEBUG_REGEX {
  244. io.write_string(common.debug_stream, "*** New thread added [PC:")
  245. common.write_padded_hex(common.debug_stream, pc, 4)
  246. io.write_string(common.debug_stream, "]\n")
  247. }
  248. vm.next_threads[vm.top_thread] = Thread{ pc = pc, saved = saved }
  249. vm.top_thread += 1
  250. case .Wait_For_Rune_Class_Negated:
  251. operand := cast(u8)vm.code[pc + size_of(Opcode)]
  252. class_data := vm.class_data[operand]
  253. next_rune := vm.next_rune
  254. check_negated: {
  255. for r in class_data.runes {
  256. if next_rune == r {
  257. break check_negated
  258. }
  259. }
  260. for range in class_data.ranges {
  261. if range.lower <= next_rune && next_rune <= range.upper {
  262. break check_negated
  263. }
  264. }
  265. add_thread(vm, saved, pc + size_of(Opcode) + size_of(u8))
  266. }
  267. when common.ODIN_DEBUG_REGEX {
  268. io.write_string(common.debug_stream, "*** New thread added [PC:")
  269. common.write_padded_hex(common.debug_stream, pc, 4)
  270. io.write_string(common.debug_stream, "]\n")
  271. }
  272. vm.next_threads[vm.top_thread] = Thread{ pc = pc, saved = saved }
  273. vm.top_thread += 1
  274. case:
  275. when common.ODIN_DEBUG_REGEX {
  276. io.write_string(common.debug_stream, "*** New thread added [PC:")
  277. common.write_padded_hex(common.debug_stream, pc, 4)
  278. io.write_string(common.debug_stream, "]\n")
  279. }
  280. vm.next_threads[vm.top_thread] = Thread{ pc = pc, saved = saved }
  281. vm.top_thread += 1
  282. }
  283. break resolution_loop
  284. }
  285. return
  286. }
  287. run :: proc(vm: ^Machine, $UNICODE_MODE: bool) -> (saved: ^[2 * common.MAX_CAPTURE_GROUPS]int, ok: bool) #no_bounds_check {
  288. when UNICODE_MODE {
  289. vm.next_rune, vm.next_rune_size = utf8.decode_rune_in_string(vm.memory)
  290. } else {
  291. if len(vm.memory) > 0 {
  292. vm.next_rune = cast(rune)vm.memory[0]
  293. vm.next_rune_size = 1
  294. }
  295. }
  296. when common.ODIN_DEBUG_REGEX {
  297. io.write_string(common.debug_stream, "### Adding initial thread.\n")
  298. }
  299. {
  300. starter_saved := new([2 * common.MAX_CAPTURE_GROUPS]int)
  301. starter_saved ^= -1
  302. add_thread(vm, starter_saved, 0)
  303. }
  304. // `add_thread` adds to `next_threads` by default, but we need to put this
  305. // thread in the current thread buffer.
  306. vm.threads, vm.next_threads = vm.next_threads, vm.threads
  307. when common.ODIN_DEBUG_REGEX {
  308. io.write_string(common.debug_stream, "### VM starting.\n")
  309. defer io.write_string(common.debug_stream, "### VM finished.\n")
  310. }
  311. for {
  312. slice.zero(vm.busy_map[:])
  313. assert(vm.string_pointer <= len(vm.memory), "VM string pointer went out of bounds.")
  314. current_rune := vm.next_rune
  315. vm.current_rune = current_rune
  316. vm.current_rune_size = vm.next_rune_size
  317. when UNICODE_MODE {
  318. vm.next_rune, vm.next_rune_size = utf8.decode_rune_in_string(vm.memory[vm.string_pointer+vm.current_rune_size:])
  319. } else {
  320. if vm.string_pointer+size_of(u8) < len(vm.memory) {
  321. vm.next_rune = cast(rune)vm.memory[vm.string_pointer+size_of(u8)]
  322. vm.next_rune_size = size_of(u8)
  323. } else {
  324. vm.next_rune = 0
  325. vm.next_rune_size = 0
  326. }
  327. }
  328. when common.ODIN_DEBUG_REGEX {
  329. io.write_string(common.debug_stream, ">>> Dispatching rune: ")
  330. io.write_encoded_rune(common.debug_stream, current_rune)
  331. io.write_byte(common.debug_stream, '\n')
  332. }
  333. thread_count := vm.top_thread
  334. vm.top_thread = 0
  335. thread_loop: for i := 0; i < thread_count; i += 1 {
  336. t := vm.threads[i]
  337. when common.ODIN_DEBUG_REGEX {
  338. io.write_string(common.debug_stream, "Thread [PC:")
  339. common.write_padded_hex(common.debug_stream, t.pc, 4)
  340. io.write_string(common.debug_stream, "] stepping on ")
  341. io.write_string(common.debug_stream, opcode_to_name(vm.code[t.pc]))
  342. io.write_byte(common.debug_stream, '\n')
  343. }
  344. #partial opcode: switch vm.code[t.pc] {
  345. case .Match:
  346. when common.ODIN_DEBUG_REGEX {
  347. io.write_string(common.debug_stream, "Thread matched!\n")
  348. }
  349. saved = t.saved
  350. ok = true
  351. break thread_loop
  352. case .Match_And_Exit:
  353. when common.ODIN_DEBUG_REGEX {
  354. io.write_string(common.debug_stream, "Thread matched! (Exiting)\n")
  355. }
  356. return nil, true
  357. case .Byte:
  358. operand := cast(rune)vm.code[t.pc + size_of(Opcode)]
  359. if current_rune == operand {
  360. add_thread(vm, t.saved, t.pc + size_of(Opcode) + size_of(u8))
  361. }
  362. case .Rune:
  363. operand := intrinsics.unaligned_load(cast(^rune)&vm.code[t.pc + size_of(Opcode)])
  364. if current_rune == operand {
  365. add_thread(vm, t.saved, t.pc + size_of(Opcode) + size_of(rune))
  366. }
  367. case .Rune_Class:
  368. operand := cast(u8)vm.code[t.pc + size_of(Opcode)]
  369. class_data := vm.class_data[operand]
  370. for r in class_data.runes {
  371. if current_rune == r {
  372. add_thread(vm, t.saved, t.pc + size_of(Opcode) + size_of(u8))
  373. break opcode
  374. }
  375. }
  376. for range in class_data.ranges {
  377. if range.lower <= current_rune && current_rune <= range.upper {
  378. add_thread(vm, t.saved, t.pc + size_of(Opcode) + size_of(u8))
  379. break opcode
  380. }
  381. }
  382. case .Rune_Class_Negated:
  383. operand := cast(u8)vm.code[t.pc + size_of(Opcode)]
  384. class_data := vm.class_data[operand]
  385. for r in class_data.runes {
  386. if current_rune == r {
  387. break opcode
  388. }
  389. }
  390. for range in class_data.ranges {
  391. if range.lower <= current_rune && current_rune <= range.upper {
  392. break opcode
  393. }
  394. }
  395. add_thread(vm, t.saved, t.pc + size_of(Opcode) + size_of(u8))
  396. case .Wildcard:
  397. add_thread(vm, t.saved, t.pc + size_of(Opcode))
  398. case .Multiline_Open:
  399. if current_rune == '\n' {
  400. // UNIX newline.
  401. add_thread(vm, t.saved, t.pc + 2 * size_of(Opcode))
  402. } else if current_rune == '\r' {
  403. if vm.next_rune == '\n' {
  404. // Windows newline. (1/2)
  405. add_thread(vm, t.saved, t.pc + size_of(Opcode))
  406. } else {
  407. // Mac newline.
  408. add_thread(vm, t.saved, t.pc + 2 * size_of(Opcode))
  409. }
  410. }
  411. case .Multiline_Close:
  412. if current_rune == '\n' {
  413. // Windows newline. (2/2)
  414. add_thread(vm, t.saved, t.pc + size_of(Opcode))
  415. }
  416. case .Wait_For_Byte:
  417. operand := cast(rune)vm.code[t.pc + size_of(Opcode)]
  418. if vm.next_rune == operand {
  419. add_thread(vm, t.saved, t.pc + size_of(Opcode) + size_of(u8))
  420. }
  421. when common.ODIN_DEBUG_REGEX {
  422. io.write_string(common.debug_stream, "*** New thread added [PC:")
  423. common.write_padded_hex(common.debug_stream, t.pc, 4)
  424. io.write_string(common.debug_stream, "]\n")
  425. }
  426. vm.next_threads[vm.top_thread] = Thread{ pc = t.pc, saved = t.saved }
  427. vm.top_thread += 1
  428. case .Wait_For_Rune:
  429. operand := intrinsics.unaligned_load(cast(^rune)&vm.code[t.pc + size_of(Opcode)])
  430. if vm.next_rune == operand {
  431. add_thread(vm, t.saved, t.pc + size_of(Opcode) + size_of(rune))
  432. }
  433. when common.ODIN_DEBUG_REGEX {
  434. io.write_string(common.debug_stream, "*** New thread added [PC:")
  435. common.write_padded_hex(common.debug_stream, t.pc, 4)
  436. io.write_string(common.debug_stream, "]\n")
  437. }
  438. vm.next_threads[vm.top_thread] = Thread{ pc = t.pc, saved = t.saved }
  439. vm.top_thread += 1
  440. case .Wait_For_Rune_Class:
  441. operand := cast(u8)vm.code[t.pc + size_of(Opcode)]
  442. class_data := vm.class_data[operand]
  443. next_rune := vm.next_rune
  444. check: {
  445. for r in class_data.runes {
  446. if next_rune == r {
  447. add_thread(vm, t.saved, t.pc + size_of(Opcode) + size_of(u8))
  448. break check
  449. }
  450. }
  451. for range in class_data.ranges {
  452. if range.lower <= next_rune && next_rune <= range.upper {
  453. add_thread(vm, t.saved, t.pc + size_of(Opcode) + size_of(u8))
  454. break check
  455. }
  456. }
  457. }
  458. when common.ODIN_DEBUG_REGEX {
  459. io.write_string(common.debug_stream, "*** New thread added [PC:")
  460. common.write_padded_hex(common.debug_stream, t.pc, 4)
  461. io.write_string(common.debug_stream, "]\n")
  462. }
  463. vm.next_threads[vm.top_thread] = Thread{ pc = t.pc, saved = t.saved }
  464. vm.top_thread += 1
  465. case .Wait_For_Rune_Class_Negated:
  466. operand := cast(u8)vm.code[t.pc + size_of(Opcode)]
  467. class_data := vm.class_data[operand]
  468. next_rune := vm.next_rune
  469. check_negated: {
  470. for r in class_data.runes {
  471. if next_rune == r {
  472. break check_negated
  473. }
  474. }
  475. for range in class_data.ranges {
  476. if range.lower <= next_rune && next_rune <= range.upper {
  477. break check_negated
  478. }
  479. }
  480. add_thread(vm, t.saved, t.pc + size_of(Opcode) + size_of(u8))
  481. }
  482. when common.ODIN_DEBUG_REGEX {
  483. io.write_string(common.debug_stream, "*** New thread added [PC:")
  484. common.write_padded_hex(common.debug_stream, t.pc, 4)
  485. io.write_string(common.debug_stream, "]\n")
  486. }
  487. vm.next_threads[vm.top_thread] = Thread{ pc = t.pc, saved = t.saved }
  488. vm.top_thread += 1
  489. case .Match_All_And_Escape:
  490. t.pc += size_of(Opcode)
  491. // The point of this loop is to walk out of wherever this
  492. // opcode lives to the end of the program, while saving the
  493. // index to the length of the string at each pass on the way.
  494. escape_loop: for {
  495. #partial switch vm.code[t.pc] {
  496. case .Match, .Match_And_Exit:
  497. break escape_loop
  498. case .Jump:
  499. t.pc = cast(int)intrinsics.unaligned_load(cast(^u16)&vm.code[t.pc + size_of(Opcode)])
  500. case .Save:
  501. index := vm.code[t.pc + size_of(Opcode)]
  502. t.saved[index] = len(vm.memory)
  503. t.pc += size_of(Opcode) + size_of(u8)
  504. case .Match_All_And_Escape:
  505. // Layering these is fine.
  506. t.pc += size_of(Opcode)
  507. // If the loop has to process any opcode not listed above,
  508. // it means someone did something odd like `a(.*$)b`, in
  509. // which case, just fail. Technically, the expression makes
  510. // no sense.
  511. case:
  512. break opcode
  513. }
  514. }
  515. saved = t.saved
  516. ok = true
  517. return
  518. case:
  519. when common.ODIN_DEBUG_REGEX {
  520. io.write_string(common.debug_stream, "Opcode: ")
  521. io.write_int(common.debug_stream, cast(int)vm.code[t.pc])
  522. io.write_string(common.debug_stream, "\n")
  523. }
  524. panic("Invalid opcode in RegEx thread loop.")
  525. }
  526. }
  527. vm.threads, vm.next_threads = vm.next_threads, vm.threads
  528. when common.ODIN_DEBUG_REGEX {
  529. io.write_string(common.debug_stream, "<<< Frame ended. (Threads: ")
  530. io.write_int(common.debug_stream, vm.top_thread)
  531. io.write_string(common.debug_stream, ")\n")
  532. }
  533. if vm.string_pointer == len(vm.memory) || vm.top_thread == 0 {
  534. break
  535. }
  536. vm.string_pointer += vm.current_rune_size
  537. }
  538. return
  539. }
  540. opcode_count :: proc(code: Program) -> (opcodes: int) {
  541. iter := Opcode_Iterator{ code, 0 }
  542. for _ in iterate_opcodes(&iter) {
  543. opcodes += 1
  544. }
  545. return
  546. }
  547. create :: proc(code: Program, str: string) -> (vm: Machine) {
  548. assert(len(code) > 0, "RegEx VM has no instructions.")
  549. vm.memory = str
  550. vm.code = code
  551. sizing := len(code) >> 6 + (1 if len(code) & 0x3F > 0 else 0)
  552. assert(sizing > 0)
  553. vm.busy_map = make([]u64, sizing)
  554. max_possible_threads := max(1, opcode_count(vm.code) - 1)
  555. vm.threads = make([^]Thread, max_possible_threads)
  556. vm.next_threads = make([^]Thread, max_possible_threads)
  557. return
  558. }