@@ -598,6 +598,79 @@ def make_asciidoc_mnemonic(instr_name, instr_data):
598598
599599 return asciidoc_content
600600
601+
602+ def process_asciidoc_block (block , instr_name ):
603+ '''
604+ Process a block of code for a specific instruction.
605+
606+ This function takes a code block and an instruction name, then creates a new block
607+ specific to that instruction. It handles the "match op" (switch) structure,
608+ extracts the relevant parts, and formats them appropriately.
609+
610+ Args:
611+ block (str): The original code block to process.
612+ instr_name (str): The name of the instruction to focus on.
613+
614+ Returns:
615+ str: A new code block formatted for the specific instruction.
616+ '''
617+
618+ new_block = []
619+ start_word = "match op"
620+ stop_word = "};"
621+ first_two_words = ""
622+ instr_upper = instr_name .upper ()
623+ instr_pattern = f"RISCV_{ instr_upper } "
624+
625+ # Split the block into lines and replace the generic TYPE with the specific instruction
626+ block_lines = block .split ('\n ' )
627+ block_lines [0 ] = re .sub (r'\b\w*TYPE\w*\b' , instr_pattern , block_lines [0 ])
628+
629+ # Find the start of the "match op" (switch) block
630+ start_block_index = - 1
631+ for i , line in enumerate (block_lines ):
632+ if start_word in line :
633+ start_block_index = i
634+ first_two_words = ' ' .join (line .split ()[:2 ])
635+ break
636+ else :
637+ new_block .append (line )
638+
639+ # Find the end of the "match op" (switch) block
640+ end_block_index = - 1
641+ for i , line in enumerate (block_lines ):
642+ if stop_word in line :
643+ end_block_index = i
644+ break
645+
646+ if (start_block_index != - 1 ) and (end_block_index != - 1 ):
647+ # Process the "match op" block
648+ block_match_op = block_lines [start_block_index :end_block_index ]
649+ encontrado = False
650+ for line in block_match_op :
651+ if instr_pattern in line :
652+ encontrado = True
653+ if encontrado :
654+ if re .search (r'\bRISCV_\w+\b' , line ) and instr_pattern not in line :
655+ break
656+ elif instr_pattern in line :
657+ # Format the line for the specific instruction
658+ partes = line .split ('=' )
659+ partes [0 ] = first_two_words .strip ()
660+ line = '=' .join (partes ).strip ()
661+ line = line .replace ("=>" , " =" )
662+ new_block .append (' ' + line )
663+ else :
664+ new_block .append (line )
665+
666+ for line in block_lines [end_block_index + 1 :]:
667+ new_block .append (line )
668+
669+ # Join the processed lines into a single string
670+ formatted_block = '\n ' .join (new_block )
671+ return formatted_block
672+
673+
601674def find_matching_brace (text , start ):
602675 count = 0
603676 for i , char in enumerate (text [start :], start ):
@@ -629,7 +702,7 @@ def make_asciidoc_sail(instr_name):
629702 asciidoc_content += f"== Instruction { instr_name } \n \n "
630703 asciidoc_content += "[source,sail]\n "
631704 asciidoc_content += "----\n "
632- asciidoc_content += block + " \n "
705+ asciidoc_content += process_asciidoc_block ( block , instr_name )
633706 asciidoc_content += "----\n \n "
634707 return asciidoc_content # Return the AsciiDoc formatted string
635708
0 commit comments