You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.
// IR 文本输出:
// - 将 IR 打印为 .ll 风格的文本
// - 支撑调试与测试对比( diff)
# include "ir/IR.h"
# include <iostream>
# include <stdexcept>
namespace ir {
static const char * TypeToString ( const Type & ty ) {
switch ( ty . kind ( ) ) {
case Type : : Kind : : Void :
return " void " ;
case Type : : Kind : : Int32 :
return " i32 " ;
}
throw std : : runtime_error ( " 未知类型 " ) ;
}
static const char * OpcodeToString ( Opcode op ) {
switch ( op ) {
case Opcode : : Add :
return " add " ;
case Opcode : : Sub :
return " sub " ;
case Opcode : : Mul :
return " mul " ;
case Opcode : : Ret :
return " ret " ;
}
return " ? " ;
}
void IRPrinter : : Print ( const Module & module ) {
for ( const auto & func : module . functions ( ) ) {
std : : cout < < " define " < < TypeToString ( * func - > type ( ) ) < < " @ "
< < func - > name ( ) < < " () { \n " ;
const auto * bb = func - > entry ( ) ;
if ( ! bb ) {
std : : cout < < " } \n " ;
continue ;
}
for ( const auto & instPtr : bb - > instructions ( ) ) {
const auto * inst = instPtr . get ( ) ;
switch ( inst - > opcode ( ) ) {
case Opcode : : Add :
case Opcode : : Sub :
case Opcode : : Mul : {
auto * bin = static_cast < const BinaryInst * > ( inst ) ;
std : : cout < < " " < < bin - > name ( ) < < " = " < < OpcodeToString ( bin - > opcode ( ) )
< < " " < < TypeToString ( * bin - > lhs ( ) - > type ( ) ) < < " "
< < bin - > lhs ( ) - > name ( ) < < " , " < < bin - > rhs ( ) - > name ( ) < < " \n " ;
break ;
}
case Opcode : : Ret : {
auto * ret = static_cast < const ReturnInst * > ( inst ) ;
std : : cout < < " ret " < < TypeToString ( * ret - > value ( ) - > type ( ) ) < < " "
< < ret - > value ( ) - > name ( ) < < " \n " ;
break ;
}
}
}
std : : cout < < " } \n " ;
}
}
} // namespace ir