Repository navigation
Expand file tree
/
Copy pathcatalyst.js
More file actions
2774 lines (2277 loc) · 89 KB
/
Copy pathcatalyst.js
File metadata and controls
2774 lines (2277 loc) · 89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// catalyst.js - JavaScript interface to Catalyst VM
class Catalyst {
constructor() {
this.translationEnabled = true
this.debugMode = true
this.stats = {
totalInvocations: 0,
translations: 0,
cachedMethods: 0,
executionTime: 0,
optimizedMethods: 0,
translationValidationsPassed: 0,
translationValidationsFailed: 0,
compilationValidationsPassed: 0,
compilationValidationsFailed: 0,
retryAttempts: 0,
retrySuccesses: 0,
llmAttempts: 0,
llmSuccesses: 0
}
// Store generated WAT for translation.
this.methodTranslations = new Map()
// Cache interpreted results for validation.
this.interpretedResults = new Map()
// Cache the last execution result.
this.lastExecutionResult = null
// Track the last logged cached result to avoid console spam.
this.lastLoggedCachedResult = null
// Track if WAT module has been logged this run.
this.watModuleLoggedThisRun = false
this.initializeSemanticAnalyzer()
}
initialize() {
if (this.vm) return this.vm
return WebAssembly.instantiateStreaming(
fetch('catalyst.wasm?' + Date.now()),
{
env: {
reportResult: (value) => {
// used by this.run()
this.onResult(value)
// Only log results in debug mode, to avoid console spam.
if (this.debugMode) console.log(`🎯 VM Result: ${value}`)
},
translateMethodForReceiverWithIdentityHash: (method, receiverIdentityHash) => {
// No return value needed, just trigger async translation if desired.
if (!this.translationEnabled) return
this.translateMethodToWASM(method, receiverIdentityHash).then(() => {
// Translation complete - function now available in function table.
if (this.debugMode) console.log(`🔥 Translation complete for receiver with identity hash ${receiverIdentityHash}.`)
})
},
debugLog: (level, message, messageLength) => {
if (this.debugMode) {
const message = this.readWASMString(message, messageLength)
console.log(`🐛 [${level}] ${message}`)
}
}
}
})
.then(wasmModule => {
this.coreWASMModule = wasmModule
this.coreWASMExports = this.coreWASMModule.instance.exports
this.vm = this.coreWASMExports.newVirtualMachine()
this.coreWASMExports.createMinimalObjectMemory(this.vm)
if (!this.vm) throw new Error('WASM VM initialization failed')
console.log('✅ Catalyst VM initialized successfully')
this.translatedMethods = this.coreWASMExports.translatedMethods
// Check proxy availability and configure LLM endpoints
return this.configureProxy()
.then(() => {
if (this.debugMode) console.log('🎯 VM initialization complete with LLM configuration')
})
.catch(error => {
if (this.debugMode) console.log('⚠️ Proxy configuration failed:', error)
})
})
.catch(error => {
console.error('❌ Failed to load WASM module:', error)
throw error
})
}
/**
* Check proxy availability and configure LLM endpoints
*/
async configureProxy() {
try {
// Check if proxy is running by making a test request to health endpoint
const testResponse = await fetch('http://localhost:8001/health', {
method: 'GET'
})
// If we get any response (even error), proxy is running
this.llmConfig.enabled = true
this.llmConfig.provider = 'anthropic' // Default to OpenAI, proxy will handle provider selection
this.llmConfig.endpoint = 'http://localhost:8001/api/anthropic/v1/messages'
this.llmConfig.model = 'claude-opus-4-20250514'
if (this.debugMode) {
console.log('🔗 API proxy detected and configured')
console.log('☁️ LLM optimization enabled via proxy')
console.log('🔧 LLM Config updated:', {
provider: this.llmConfig.provider,
model: this.llmConfig.model,
endpoint: this.llmConfig.endpoint,
enabled: this.llmConfig.enabled,
proxyManaged: true
})
console.log('💡 Provider selection and authentication handled by proxy')
}
} catch (error) {
// Proxy not available - try health endpoint as fallback
try {
const testResponse = await fetch('http://localhost:8001/health', {
method: 'GET'
})
this.llmConfig.enabled = true
this.llmConfig.provider = 'anthropic'
this.llmConfig.endpoint = 'http://localhost:8001/api/anthropic/v1/messages'
this.llmConfig.model = 'claude-opus-4-20250514'
if (this.debugMode) {
console.log('🔗 API proxy detected (Anthropic endpoint)')
console.log('☁️ LLM optimization enabled via proxy')
}
} catch (anthropicError) {
// No proxy available
if (this.debugMode) {
console.log('⚠️ API proxy not available:', error.message)
console.log('🔄 LLM optimization will remain disabled')
console.log('💡 Start proxy with: npm run proxy')
}
}
}
}
async resetVM() {
// Re-initialize the WASM module to reset the VM state
await this.initialize()
if (this.debugMode) console.log('🔄 VM state reset')
}
async run() {
if (!this.vm) return Promise.reject(new Error('VM not initialized. Call initialize() first.'))
// Do NOT reset the VM here; we want to preserve state across runs.
return new Promise((resolve, reject) => {
let receivedResult = null
// Temporarily override onResult to capture the value.
const prevOnResult = this.onResult
this.onResult = (value) => {
receivedResult = value
}
try {
// Run the VM interpreter (this will execute the benchmark example)
this.coreWASMExports.resetMinimalMemory(this.vm)
const result = this.coreWASMExports.interpret(this.vm)
this.stats.totalInvocations++
// Cache the execution result for LLM validation
if (receivedResult !== null) {
this.lastExecutionResult = receivedResult
// Only log if this is a new/different result
if (this.debugMode && receivedResult !== this.lastLoggedCachedResult) {
console.log(`🔍 Cached execution result: ${receivedResult}`)
this.lastLoggedCachedResult = receivedResult
}
}
// Restore previous onResult
this.onResult = prevOnResult
resolve({
success: result !== 0,
translations: this.stats.translations,
// Use the actual result received
results: [receivedResult]
})
}
catch (error) {
this.onResult = prevOnResult
console.error('❌ VM execution failed:', error)
reject(error)
}
})
}
// method translation: Translate bytecode to WAT, compile to WASM function.
async translateMethodToWASM(method, receiverIdentityHash) {
console.log(`🔍 [TRANSLATION DEBUG] translateMethodToWASM called: receiver identity hash=${receiverIdentityHash}, translationEnabled=${this.translationEnabled}`)
try {
const watCode = await this.translateMethodToWAT(method, receiverIdentityHash)
// Check if translation failed (LLM translation not available)
if (!watCode) {
if (this.debugMode) {
console.log(`❌ Translation failed - no optimized WAT available.`)
console.log(`🔄 Method will continue being interpreted`)
}
// Return 0 to indicate no translated method available
return 0
}
// Compile WAT to WASM function and store in function table
const compiledFunction = await this.compileWATToFunction(watCode)
// Get the next available function table index
const functionIndex = ++this.stats.translations // Start at index 1
// Store the compiled function in the function table
this.translatedMethods.set(functionIndex, compiledFunction)
// Mark method as compiled
this.methodTranslations.set(method, compiledFunction)
// Set the compiledFunction field in the WASM $CompiledMethod struct
this.coreWASMExports.setMethodFunctionIndex(method, functionIndex)
const compilationTime = performance.now() - this.startTime
this.stats.cachedMethods++
// Debug log for translation
console.log(`[TRANSLATION] Translated method to functionIndex=${functionIndex} in ${compilationTime.toFixed(2)}ms`)
if (this.debugMode) {
console.log(`🔥 Translated method in ${compilationTime.toFixed(2)}ms.`)
console.log(`📍 Stored in function table at index ${functionIndex}`)
}
// Return function table index for WASM to use with call_indirect
return functionIndex
}
catch (error) {
console.error('❌ Translation failed:', error)
// Return 0 to indicate no translated method available -
// VM will fall back to interpretation.
return 0
}
}
// Translate Squeak bytecode to WAT (WebAssembly Text format)
async translateMethodToWAT(method, receiverIdentityHash) {
if (!this.translationEnabled) return 0
this.startTime = performance.now()
console.log(`🔍 [TRANSLATE DEBUG] translateMethodToWAT available'`)
// Get or generate interpreted result for this method
let cachedInterpretedResult = method ? this.interpretedResults.get(method) : null
if (!cachedInterpretedResult && method) {
// Execute this method in interpretation mode to get the reference result
try {
// Use receiver = 100
cachedInterpretedResult = await this.executeMethodInterpreted(method, 100)
if (cachedInterpretedResult !== null) {
this.interpretedResults.set(method, cachedInterpretedResult)
if (this.debugMode) {
console.log(`🔍 Cached new interpreted result for method: ${cachedInterpretedResult}`)
}
}
}
catch (error) {
if (this.debugMode) console.log(`⚠️ Failed to execute method in interpretation mode: ${error}`)
}
}
if (this.debugMode) console.log(`🔍 Using interpreted result for receiver with identity hash ${receiverIdentityHash}:`, cachedInterpretedResult)
// Try cloud-powered semantic optimization
const bytecodesObject = this.coreWASMExports.methodBytecodes(method)
const optimization = await this.analyzeAndOptimize(
Array.from(
new Uint8Array(
this.coreWASMExports.bytes.buffer,
this.coreWASMExports.copyByteArrayToMemory(bytecodesObject),
this.coreWASMExports.byteArrayLength(bytecodesObject))),
method,
cachedInterpretedResult)
if (optimization.optimized) {
if (this.debugMode) {
console.log(`🚀 Generated LLM-optimized WAT for: ${optimization.description.summary}`)
console.log(`☁️ Pattern: ${optimization.pattern}`)
console.log(`📋 Analysis:`, optimization.description)
}
return optimization.watCode
}
// LLM optimization failed - fail the entire translation
// This forces the VM to continue interpreting rather than caching suboptimal WAT
if (this.debugMode) {
console.log(`❌ Method translation failed - LLM optimization not available`)
console.log(`📋 Method analysis:`, optimization.description)
console.log(`🔄 VM will continue interpreting this method`)
}
// Return null to indicate translation failure
return null
}
// ==================== SEMANTIC ANALYZER ====================
initializeSemanticAnalyzer() {
// Configure LLM endpoint for cloud-based optimization via proxy
this.llmConfig = {
enabled: false, // Will be enabled when proxy is detected
provider: null, // Will be set to 'openai' or 'anthropic' when proxy is available
endpoint: null, // Will be set to proxy endpoint
model: null // Will be set based on provider
}
// No hardcoded patterns - we analyze everything generically!
this.bytecodeNames = {
0x00: 'push_field_0', 0x01: 'push_field_1', 0x02: 'push_field_2', 0x03: 'push_field_3',
0x10: 'push_temp_0', 0x11: 'push_temp_1', 0x12: 'push_temp_2', 0x13: 'push_temp_3',
0x20: 'push_literal_0', 0x21: 'push_literal_1', 0x22: 'push_literal_2', 0x23: 'push_literal_3',
0x24: 'push_literal_4', 0x25: 'push_literal_5', 0x26: 'push_literal_6', 0x27: 'push_literal_7',
0x28: 'push_literal_8', 0x29: 'push_literal_9', 0x2a: 'push_literal_10', 0x2b: 'push_literal_11',
0x70: 'push_receiver', 0x71: 'push_true', 0x72: 'push_false', 0x73: 'push_nil',
0xB0: 'add', 0xB1: 'subtract', 0xB2: 'less_than', 0xB3: 'greater_than',
0xB8: 'multiply', 0xB9: 'divide', 0xBA: 'modulo', 0xBB: 'equals',
0x7C: 'return_top', 0x7D: 'return_receiver', 0xD0: 'send_message'
}
}
/**
* Main semantic analysis entry point - Cloud-powered optimization
*/
async analyzeAndOptimize(bytecodes, method, cachedInterpretedResult) {
// ALWAYS log this to trace literal extraction issues
console.log(`🔍 [ANALYZE DEBUG] analyzeAndOptimize called with method:`, method ? 'available' : 'null')
// Clear literal cache at start of new analysis to prevent redundant fetches
this.clearLiteralCache()
if (this.debugMode) console.log(`🔬 Analyzing bytecodes:`, bytecodes.map(b => `0x${b.toString(16)}`))
// Generate English description of what the method does
const description = this.describeBytecodeSequence(bytecodes)
if (this.debugMode) {
console.log(`📝 Method description: ${description.summary}`)
console.log(`🎯 Operations: ${description.operations.join(' → ')}`)
}
// Try cloud-based optimization if enabled
if (this.debugMode) {
console.log(`🔧 LLM Config check:`, {
enabled: this.llmConfig.enabled,
provider: this.llmConfig.provider,
endpoint: this.llmConfig.endpoint,
isOptimizable: this.isOptimizable(description)
})
}
if (this.llmConfig.enabled && this.isOptimizable(description)) {
if (this.debugMode) console.log(`🚀 Attempting LLM optimization...`)
const watCode = await this.generateOptimizedWATWithLLM(description, bytecodes, method, cachedInterpretedResult)
if (watCode) {
if (this.debugMode) {
console.log(`☁️ LLM-generated optimized WAT received`)
console.log(`📝 Generated WAT code:\n${watCode}`)
console.log(`🔍 Validating optimization correctness...`)
}
// CRITICAL: Validate the optimization before accepting it
const validationResult = await this.validateOptimization(bytecodes, watCode, method, cachedInterpretedResult)
if (validationResult.valid) {
this.stats.optimizedMethods++
if (this.debugMode) console.log(`✅ Optimization validation PASSED - results match!`)
return {
optimized: true,
pattern: 'llm_optimized',
description: description,
watCode: watCode
}
}
else {
if (this.debugMode) console.log(`❌ Optimization validation FAILED - trying once more with feedback...`)
// Try once more with better feedback, using cached interpreted result
this.stats.retryAttempts++
const retryWatCode = await this.generateOptimizedWATWithRetry(description, bytecodes, watCode, method)
if (retryWatCode) {
const retryValidationResult = await this.validateOptimization(bytecodes, retryWatCode, method, cachedInterpretedResult)
if (retryValidationResult.valid) {
this.stats.optimizedMethods++
this.stats.retrySuccesses++
if (this.debugMode) {
console.log(`✅ Retry optimization validation PASSED - results match!`)
}
return {
optimized: true,
pattern: 'llm_optimized_retry',
description: description,
watCode: retryWatCode
}
}
}
// Fall through to interpretation
if (this.debugMode) console.log(`❌ Both optimization attempts failed - falling back to bytecode interpretation`)
}
}
}
// Fallback to interpretation
if (this.debugMode) console.log(`⚠️ Using bytecode interpretation`)
return {
optimized: false,
pattern: 'interpreted',
description: description,
watCode: null
}
}
// ==================== UNIVERSAL BYTECODE ANALYSIS ====================
/**
* Generate English description of any bytecode sequence
* This replaces all hardcoded pattern matching
*/
describeBytecodeSequence(bytecodes) {
const operations = bytecodes.map(b => this.bytecodeNames[b] || `unknown_0x${b.toString(16)}`)
const analysis = this.analyzeBytecodeStructure(bytecodes)
return {
summary: '',
operations: operations,
analysis: analysis,
bytecodes: bytecodes.map(b => `0x${b.toString(16)}`),
complexity: this.estimateComplexity(analysis),
canOptimize: this.canOptimize(analysis)
}
}
clearLiteralCache() {
if (this.literalCache) {
this.literalCache.clear()
if (this.debugMode) { console.log('🗑️ Literal cache cleared') }
}
}
/**
* Analyze structural patterns in bytecode (no hardcoded patterns!)
*/
analyzeBytecodeStructure(bytecodes) {
const analysis = {
hasArithmetic: false,
hasLiterals: false,
hasFieldAccess: false,
hasMethodCalls: false,
literalValues: [],
stackOperations: 0,
returnType: 'unknown',
isLeaf: true,
isPure: true
}
for (const bytecode of bytecodes) {
// Arithmetic operations
if (bytecode >= 0xB0 && bytecode <= 0xBB) analysis.hasArithmetic = true
// Literal pushes
if (bytecode >= 0x20 && bytecode <= 0x2F) {
analysis.hasLiterals = true
analysis.literalValues.push(bytecode - 0x20)
}
// Field access
if (bytecode >= 0x00 && bytecode <= 0x0F) analysis.hasFieldAccess = true
// Method calls
if (bytecode === 0xD0) {
analysis.hasMethodCalls = true
analysis.isLeaf = false
analysis.isPure = false
}
// Stack operations
if (bytecode === 0x70 || (bytecode >= 0x00 && bytecode <= 0x2F))
analysis.stackOperations++
// Return types
if (bytecode === 0x7C) analysis.returnType = 'top_of_stack'
if (bytecode === 0x7D) analysis.returnType = 'receiver'
}
return analysis
}
/**
* Estimate computational complexity
*/
estimateComplexity(analysis) {
if (analysis.hasMethodCalls) return "O(n) - depends on called methods"
if (analysis.hasFieldAccess) return "O(1) - single memory access"
return "O(1) - constant time"
}
/**
* Determine if method is worth optimizing with LLM
*/
canOptimize(analysis) {
const canOpt = analysis.isLeaf && // No method calls
(analysis.hasArithmetic || analysis.hasFieldAccess) && // Has optimizable operations
analysis.stackOperations <= 50 // Allow much more complex computations
if (this.debugMode) {
console.log(`🔍 Can optimize analysis:`, {
isLeaf: analysis.isLeaf,
hasArithmetic: analysis.hasArithmetic,
hasFieldAccess: analysis.hasFieldAccess,
stackOperations: analysis.stackOperations,
result: canOpt
})
}
return canOpt
}
isOptimizable(description) {
const isOptimizable = description.canOptimize &&
description.operations.length <= 100 && // Allow much more complex computations
!description.summary.includes('unknown_')
if (this.debugMode) {
console.log(`🤔 Optimizable check:`, {
canOptimize: description.canOptimize,
operationsLength: description.operations.length,
hasUnknown: description.summary.includes('unknown_'),
result: isOptimizable
})
}
return isOptimizable
}
/**
* Generate detailed English interpretation of bytecode sequence
*/
generateEnglishInterpretation(bytecodes, method) {
const interpretations = []
for (let i = 0; i < bytecodes.length; i++) {
const bytecode = bytecodes[i]
const hexCode = `0x${bytecode.toString(16).padStart(2, '0')}`
let interpretation = ''
switch (bytecode) {
case 0x70: // Push receiver
interpretation = 'Push the receiver (self) onto the Smalltalk context stack'
break
case 0x71: // Push true
interpretation = 'Push the boolean value true onto the Smalltalk context stack'
break
case 0x72: // Push false
interpretation = 'Push the boolean value false onto the Smalltalk context stack'
break
case 0x73: // Push nil
interpretation = 'Push the nil value onto the Smalltalk context stack'
break
// Field access
case 0x00: case 0x01: case 0x02: case 0x03:
case 0x04: case 0x05: case 0x06: case 0x07:
case 0x08: case 0x09: case 0x0A: case 0x0B:
case 0x0C: case 0x0D: case 0x0E: case 0x0F:
const fieldIndex = bytecode - 0x00
interpretation = `Push field ${fieldIndex} from the receiver onto the Smalltalk context stack`
break
// Temporary variables
case 0x10: case 0x11: case 0x12: case 0x13:
case 0x14: case 0x15: case 0x16: case 0x17:
case 0x18: case 0x19: case 0x1A: case 0x1B:
case 0x1C: case 0x1D: case 0x1E: case 0x1F:
const tempIndex = bytecode - 0x10
interpretation = `Push temporary variable ${tempIndex} onto the Smalltalk context stack`
break
// Literal constants
case 0x20: case 0x21: case 0x22: case 0x23:
case 0x24: case 0x25: case 0x26: case 0x27:
case 0x28: case 0x29: case 0x2A: case 0x2B:
case 0x2C: case 0x2D: case 0x2E: case 0x2F:
const literalIndex = bytecode - 0x20
interpretation = `Push context literal ${literalIndex} onto the Smalltalk context stack`
break
// Arithmetic operations
case 0xB0: // Add
interpretation = 'Pop two values from the Smalltalk context stack, add them, push result back'
break
case 0xB1: // Subtract
interpretation = 'Pop two values from the Smalltalk context stack, subtract second from first, push result back'
break
case 0xB2: // Less than
interpretation = 'Pop two values from the Smalltalk context stack, check if first < second, push boolean result'
break
case 0xB3: // Greater than
interpretation = 'Pop two values from the Smalltalk context stack, check if first > second, push boolean result'
break
case 0xB8: // Multiply
interpretation = 'Pop two values from the Smalltalk context stack, multiply them, push result back'
break
case 0xB9: // Divide
interpretation = 'Pop two values from the Smalltalk context stack, divide first by second, push result back'
break
case 0xBA: // Modulo
interpretation = 'Pop two values from the Smalltalk context stack, calculate first modulo second, push result back'
break
case 0xBB: // Equals
interpretation = 'Pop two values from the Smalltalk context stack, check if they are equal, push boolean result'
break
// Returns
case 0x7C: // Return top of stack
interpretation = 'Return the top value from the stack as the method result'
break
case 0x7D: // Return receiver
interpretation = 'Return the receiver (self) as the method result'
break
// Message sends
case 0xD0: // Send message
interpretation = 'Send a message to the receiver on top of stack (method call)'
break
default:
interpretation = `Unknown bytecode instruction - consult Squeak VM documentation`
break
}
interpretations.push(`${i + 1}. ${hexCode}: ${interpretation}`)
}
return interpretations.join('\n')
}
/**
* Generate step-by-step stack analysis for the LLM
*/
generateStackAnalysis(operations, method) {
const stack = []
let receiver = 'R' // Use 'R' to represent receiver in analysis
let steps = []
for (let i = 0; i < operations.length; i++) {
const op = operations[i]
let stackBefore = [...stack]
if (op.startsWith('push_literal_')) {
const literalIndex = parseInt(op.split('_')[2])
let literalValue = `literals[${literalIndex}]`
stack.push(literalValue)
}
else if (op === 'push_receiver') stack.push(receiver)
else if (op === 'multiply') {
if (stack.length >= 2) {
const b = stack.pop()
const a = stack.pop()
stack.push(`(${a}*${b})`)
}
}
else if (op === 'add') {
if (stack.length >= 2) {
const b = stack.pop()
const a = stack.pop()
stack.push(`(${a}+${b})`)
}
}
else if (op === 'return_top') {
// Don't modify stack for return
}
// Log the step
steps.push(`Step ${i + 1}: ${op} → Stack: [${stack.join(', ')}]`)
}
const finalResult = stack.length > 0 ? stack[stack.length - 1] : '0'
steps.push(`FINAL RESULT: ${finalResult}`)
return steps.join('\n')
}
/**
* Generate optimized WAT with retry and specific feedback
*/
async generateOptimizedWATWithRetry(description, bytecodes, failedWatCode, method) {
try {
// Get the error details from the failed attempt
const errorInfo = await this.analyzeWATError(failedWatCode)
// Build a retry prompt with specific feedback
const retryPrompt = this.buildRetryPrompt(description, bytecodes, failedWatCode, errorInfo, method)
// Make the API call with retry-specific prompt
const response = await this.callLLMAPI(retryPrompt)
const retryWatCode = this.extractWATFromResponse(response)
if (retryWatCode) {
// Check if LLM generated the same WAT code as the failed attempt
if (failedWatCode && retryWatCode.trim() === failedWatCode.trim()) {
throw new Error(`LLM generated identical WAT code in retry attempt. Previous attempt failed with: ${errorInfo}. LLM is not making progress.`)
}
this.stats.retrySuccesses++
if (this.debugMode) console.log(`🔄 Retry attempt succeeded - new WAT generated`)
return retryWatCode
}
else {
if (this.debugMode) console.log(`❌ Retry attempt failed - no valid WAT extracted`)
return null
}
}
catch (error) {
if (this.debugMode) console.log(`❌ Retry attempt failed with error: ${error}`)
return null
}
}
/**
* Analyze what went wrong with the WAT code
*/
async analyzeWATError(watCode) {
try {
// Try to compile the WAT to get specific error details
if (!window.wasmTools) await this.loadWasmTools()
const fullWatModule = this.buildFullWatModule(watCode)
// Try parsing to distinguish between parsing and validation errors
let wasmBytes
try {
wasmBytes = window.wasmTools.parseWat(fullWatModule)
}
catch (parseError) {
return `WAT parsing error: ${parseError}`
}
// Try validation
try {
const isValid = window.wasmTools.validate(wasmBytes)
if (!isValid) return "WASM validation error: Binary validation failed"
}
catch (validationError) {
return `WASM validation error: ${validationError}`
}
return "No errors detected"
}
catch (error) {
return `Compilation error: ${error}`
}
}
/**
* Build minimal retry prompt with only error feedback and original WAT
*/
buildRetryPrompt(description, bytecodes, failedWatCode, errorInfo, method) {
// For retry prompts, we don't have the detailed error type info, so we'll use generic labeling
const prompt = `There were problems with that WAT code.
ERROR: ${errorInfo}
ORIGINAL WAT:
${failedWatCode}
Please fix the errors above and generate ONLY the corrected function definition.`
// Log the retry prompt to console
console.log('\n📝 ===== RETRY PROMPT =====')
console.log(prompt)
console.log('===== END RETRY PROMPT =====\n')
return prompt
}
/**
* Validate that the LLM-generated WAT produces the same result as the original bytecode
* Uses cached interpreted result to avoid recomputing the reference value
*/
async validateOptimization(bytecodes, watCode, method, cachedInterpretedResult) {
console.log(`🚨 VALIDATION STARTED - validateOptimization called`)
console.log(`📋 Parameters: bytecodes=${bytecodes?.length}, watCode=${watCode ? 'present' : 'null'}, method=${method ? 'present' : 'null'}, cachedInterpretedResult=${cachedInterpretedResult}`)
try {
// Test with a known receiver value
const testReceiver = 100
console.log(`🧪 Starting validation with receiver = ${testReceiver}`)
console.log(`📋 Method context: ${method ? 'available' : 'null'}`)
// 1. Use cached interpreted result from actual WASM VM execution
let interpretedResult = cachedInterpretedResult
if (interpretedResult === null || interpretedResult === undefined) {
console.log(`⚠️ No cached interpreted result available - using last execution result`)
// Fallback to last execution result if available
const fallbackResult = this.lastExecutionResult
if (fallbackResult === null || fallbackResult === undefined)
throw new Error('No interpreted result available for validation')
interpretedResult = fallbackResult
}
console.log(`📊 Using interpreted result from WASM VM: ${interpretedResult}`)
// 2. Compile and execute WAT optimization
console.log(`🤖 Executing WAT optimization...`)
const optimizedResult = await this.executeWATOptimized(watCode, method)
console.log(`🤖 WAT optimization returned: ${optimizedResult}`)
// 3. Compare results
const resultsMatch = interpretedResult === optimizedResult
// Track validation statistics
if (resultsMatch) this.stats.translationValidationsPassed++
else this.stats.translationValidationsFailed++
// ALWAYS show validation results clearly on console
console.log(`🔍 ===== VALIDATION RESULTS =====`)
console.log(`📊 Expected Value (Interpreted): ${interpretedResult}`)
console.log(`🤖 Computed Value (LLM-WAT): ${optimizedResult}`)
console.log(`✅ Results Match: ${resultsMatch ? 'YES' : 'NO'}`)
console.log(`📈 Validation Stats: ${this.stats.translationValidationsPassed} passed, ${this.stats.translationValidationsFailed} failed`)
console.log(`===== END VALIDATION RESULTS =====`)
return {
valid: resultsMatch,
// Return the interpreted result for caching
interpretedResult: interpretedResult
}
}
catch (error) {
console.log(`❌ ===== VALIDATION ERROR =====`)
console.log(`💥 Validation failed due to error: ${error}`)
console.log(`📊 Expected Value (Interpreted): ${cachedInterpretedResult || this.lastExecutionResult}`)
console.log(`🤖 Computed Value (LLM-WAT): ERROR - ${error.message}`)
console.log(`✅ Results Match: NO (ERROR)`)
console.log(`===== END VALIDATION ERROR =====`)
return {
valid: false,
// Return the cached result even on failure
interpretedResult: cachedInterpretedResult || this.lastExecutionResult
}
}
}
/**
* Execute a specific method through the WASM VM interpreter
* to get reference result.
*/
async executeMethodInterpreted(method, receiver) {
// Use the cached result from the last VM execution. This
// assumes the method being translated is the one that
// produced the last result.
return this.lastExecutionResult
}
/**
* Execute WAT-optimized code for validation by installing it in
* the VM's method cache, setting the method's $compiledFunc
* field, running the VM normally, then comparing results
*/
async executeWATOptimized(watCode, method) {
try {
if (!this.coreWASMModule || !this.coreWASMModule.instance) throw new Error('WASM VM not initialized')
console.log(`🔧 Installing LLM-optimized method in VM method cache for validation`)
// Compile the WAT code to a function
const compiledFunction = await this.compileWATToFunction(watCode)
// Store the compiled function in the VM's method cache,
// and set the method's $compiledFunctionIndex field correctly.
let originalCompiledFunctionIndex = null
// Save original compiled function index
originalCompiledFunctionIndex = this.coreWASMExports.getMethodFunctionIndex(method)
const originalFunction = this.translatedMethods.get(originalCompiledFunctionIndex)
const functionIndex = this.stats.translations + 1
// Set the new compiled function index
this.translatedMethods.set(functionIndex, compiledFunction)
this.coreWASMExports.setMethodFunctionIndex(method, functionIndex)
console.log(`🔧 Temporarily installed LLM function at index ${functionIndex}`)
// Run the VM normally - it will use the cached compiled function
const vmResult = await this.run()
// Extract the result value
const actualResult = vmResult.results?.[0]
console.log(`🔧 VM executed with LLM function, result: ${actualResult}`)
// Restore the original state
this.coreWASMExports.setMethodFunctionIndex(method, originalCompiledFunctionIndex)
console.log(`🔧 Restored original compiled function index ${originalCompiledFunctionIndex} for method`)
// Restore original function table entry
this.translatedMethods.set(originalCompiledFunctionIndex, originalFunction)
if (actualResult === null || actualResult === undefined) throw new Error('VM execution produced null/undefined result')
return actualResult
}
catch (error) {
if (this.debugMode) console.log(`⚠️ WAT validation execution failed: ${error}`)
throw new Error(`WAT validation execution failed: ${error.message || error}`)
}
}
// ==================== CLOUD-POWERED WAT GENERATION ====================
/**
* Generate optimized WAT using single LLM with detailed failure feedback.
*/
async generateOptimizedWATWithLLM(description, bytecodes, method, cachedInterpretedResult) {
if (!this.llmConfig.enabled) {
if (this.debugMode) console.log('⚠️ LLM optimization disabled - proxy not available')
return null
}
const maxAttempts = 2
let failureHistory = []
for (let attempt = 1; attempt <= maxAttempts; attempt++) {
this.stats.llmAttempts++
if (this.debugMode) console.log(`🤖 LLM attempt ${attempt}/${maxAttempts}`)
// Build prompt - full context for first attempt, error feedback for retries
const prompt = this.buildLLMPromptWithFailureHistory(
description,
bytecodes,
method,
attempt,
failureHistory)
const response = await this.callLLMAPI(prompt)
if (!response) {
if (this.debugMode) console.log(`❌ LLM API call failed on attempt ${attempt}`)
continue
}
// Generate WAT code from response
const watCode = this.extractWATFromResponse(response)
if (!watCode) {
if (this.debugMode) console.log(`❌ WAT generation failed on attempt ${attempt}`)
continue
}
// Check if LLM generated the same WAT code as previous attempt
if (failureHistory.length > 0) {
const lastFailure = failureHistory[failureHistory.length - 1]
if (lastFailure.watCode && watCode.trim() === lastFailure.watCode.trim()) {
throw new Error(`LLM generated identical WAT code on attempt ${attempt}. Previous attempt failed with: ${lastFailure.error}. LLM is not making progress.`)
}
}
// CRITICAL: Validate the WASM compilation and generate dump for feedback
const wasmValidationResult = await this.validateWASMCompilation(watCode)
if (!wasmValidationResult.valid) {