コンパイラ作成(74) char型の乗除余算
今回の目標
引き続きchar型のサポート。
// char型 int main() { char a = 11, b = 4, c; c = a * b; printf("%hhu * %hhu = %hhu\n", a, b, c); c = a / b; printf("%hhu / %hhu = %hhu\n", a, b, c); c = a % b; printf("%hhu %% %hhu = %hhu\n", a, b, c); }
printfの%hhuってのはC99から追加になったやつだよ。
codegen_els
乗除余算の処理のところに数行追加。
elsif op.str == "*" || op.str == "/" || op.str == "%" then mov = "mov" if type_r == "char" then mov = "movsx" end if type_l == "char" then codegen " movsx eax, al" end if str != "r10d" then codegen " #{mov} r10d, " + str end codegen " mov r11, rdx" if op.str == "/" || op.str == "%" then codegen " cdq" end codegen " " + ostr + " r10d" if op.str == "%" then codegen " mov eax, edx" end codegen " mov rdx, r11"
clang先輩のコンパイル結果を参考に修正。movsxで32bitにしてから演算してる。
動作テスト
~/myc$ myc p4.myc ~/myc$ ./p4 11 * 4 = 44 11 / 4 = 2 11 % 4 = 3 ~/myc$
OKだね。これで四則演算はできたよ。残りは比較演算と単項マイナスか。今日もあんまり進まなかったなあ。時間が取れないってのもあるけど、char型のサポートはひたすら地味で面倒ってのもあるなあ。こういう作業は苦手だよ。