4646import vadl .lcb .passes .isaMatching .database .Query ;
4747import vadl .lcb .template .CommonVarNames ;
4848import vadl .lcb .template .LcbTemplateRenderingPass ;
49+ import vadl .lcb .template .utils .ImmediatePredicateFunctionProvider ;
4950import vadl .pass .PassResults ;
5051import vadl .template .Renderable ;
5152import vadl .types .BuiltInTable ;
@@ -283,7 +284,23 @@ private Optional<String> getJumpFromMachineInstructions(
283284 .map (Definition ::simpleName );
284285 }
285286
286- record BranchInstruction (String name , /* size of the immediate */ int bitWidth ) implements
287+ record BranchInstruction (String name ,
288+ int bitWidth , /* size of the immediate */
289+ String predicateMethod ) implements
290+ Renderable {
291+
292+ @ Override
293+ public Map <String , Object > renderObj () {
294+ return Map .of (
295+ "name" , name ,
296+ "bitWidth" , bitWidth ,
297+ "predicateMethod" , predicateMethod
298+ );
299+ }
300+ }
301+
302+ record PseudoBranchInstruction (String name ,
303+ int bitWidth /* size of the immediate */ ) implements
287304 Renderable {
288305
289306 @ Override
@@ -370,7 +387,9 @@ protected Map<String, Object> createVariables(final PassResults passResults,
370387 map .put ("additionImm" , additionRI .simpleName ());
371388 map .put ("addition" , additionRR .simpleName ());
372389 map .put ("additionRegisterFile" , additionRegisterFile .simpleName ());
373- map .put ("branchInstructions" , getBranchInstructions (specification , passResults , fieldUsages ));
390+ map .put ("machineBranchInstructions" , machineBranchInstructions (specification , passResults ));
391+ map .put ("pseudoBranchInstructions" ,
392+ pseudoBranchInstructions (specification , passResults , fieldUsages ));
374393 map .put ("instructionSizes" , instructionSizes (specification ));
375394 map .put ("jumpInstruction" , jumpInstructionName );
376395 map .put ("beq" ,
@@ -415,14 +434,24 @@ private List<InstructionSize> instructionSizes(Specification specification) {
415434 .toList ();
416435 }
417436
418- private List <BranchInstruction > getBranchInstructions (
437+ private List <BranchInstruction > machineBranchInstructions (
438+ Specification specification ,
439+ PassResults passResults ) {
440+ var branchInstructions = new ArrayList <BranchInstruction >();
441+ var database = new Database (passResults , specification );
442+
443+ machineInstructions (passResults , database , branchInstructions );
444+
445+ return branchInstructions ;
446+ }
447+
448+ private List <PseudoBranchInstruction > pseudoBranchInstructions (
419449 Specification specification ,
420450 PassResults passResults ,
421451 IdentifyFieldUsagePass .ImmediateDetectionContainer fieldUsages ) {
422- var branchInstructions = new ArrayList <BranchInstruction >();
452+ var branchInstructions = new ArrayList <PseudoBranchInstruction >();
423453 var database = new Database (passResults , specification );
424454
425- machineInstructions (database , branchInstructions );
426455 pseudoInstructions (fieldUsages , database , branchInstructions );
427456
428457 return branchInstructions ;
@@ -431,7 +460,7 @@ private List<BranchInstruction> getBranchInstructions(
431460 private static void pseudoInstructions (
432461 IdentifyFieldUsagePass .ImmediateDetectionContainer fieldUsages ,
433462 Database database ,
434- List <BranchInstruction > branchInstructions ) {
463+ List <PseudoBranchInstruction > branchInstructions ) {
435464 var result = database .run (
436465 new Query .Builder ().pseudoInstructionLabel (PseudoInstructionLabel .J ).build ());
437466
@@ -447,14 +476,17 @@ private static void pseudoInstructions(
447476 var immediate = unwrap (immediates .stream ().findFirst ());
448477 int bitWidth = immediate .size ();
449478 branchInstructions .add (
450- new BranchInstruction (pseudoInstruction .identifier .simpleName (), bitWidth ));
479+ new PseudoBranchInstruction (pseudoInstruction .identifier .simpleName (), bitWidth ));
451480 }
452481 }
453482 }
454483
455484 private static void machineInstructions (
485+ PassResults passResults ,
456486 Database database ,
457487 List <BranchInstruction > branchInstructions ) {
488+ var lookup =
489+ ImmediatePredicateFunctionProvider .predicateFunctionsByFieldAccess (passResults );
458490 var result = database .run (new Query .Builder ().machineInstructionLabels (List .of (
459491 MachineInstructionLabel .J ,
460492 MachineInstructionLabel .BEQ ,
@@ -490,9 +522,14 @@ private static void machineInstructions(
490522 () -> Diagnostic .error ("We only support branch instructions with one label." ,
491523 machineInstruction .location ()));
492524 var immediate = unwrap (immediates .stream ().findFirst ());
525+ var function = ensureNonNull (lookup .get (immediate .fieldAccess ()),
526+ () -> Diagnostic .error ("Cannot find field access' predicate" , immediate .location ()));
527+ var functionName = function .header ().functionName ().lower ();
493528 int bitWidth = immediate .fieldAccess ().type ().asDataType ().bitWidth ();
494529 branchInstructions .add (
495- new BranchInstruction (machineInstruction .identifier .simpleName (), bitWidth ));
530+ new BranchInstruction (machineInstruction .identifier .simpleName (),
531+ bitWidth ,
532+ functionName ));
496533 }
497534 }
498535
0 commit comments