コンパイラ作成(107) 配列と単項演算子の優先順位

今回の目標

配列を実装したときに先送りしちゃった件をなんとかするよ。

// 配列
int main()
{
    int a[10];
    a[0] = 42;
    printf("-a[0] = %d\n", -a[0]);
}

本来は-(a[0])と評価しなくちゃいけないんだけど、(-a)[0]って解釈しちゃっててコンパイルに失敗してる。

read_modify_el

優先順位を正しくするために配列の処理を先に持って行ったよ。

  # 式の最後までのトークンを読み込み、変形する
  def read_modify_el(fkind,fstr,skind,sstr)
    el, kind, str = read_el fkind, fstr, skind, sstr
    puts to_str(el) if $opt_d   # デバッグ用
    el = modify_high_priority_el el, ["[]"]
    el = modify_el_unaryop el, ["+","-","&","*"]
    el = modify_el el, ["[]"]
    el = modify_el el, ["*","/","%"]
    el = modify_el el, ["+","-"]
    el = modify_el el, ["<",">","<=",">="]
    el = modify_el el, ["==","!="]
    el = modify_el el, ["="], :r_to_l
    puts to_str(el) if $opt_d   # デバッグ用
    return el, kind, str
  end

最初はmodify_elを修正しようとしたんだけど、途中で気が変わって別メソッドで処理することにしたよ。

modify_high_priority_el

これがその追加したメソッド。

  # elの変形(単行演算子より優先順位が高い演算子の処理)
  def modify_high_priority_el(el, opl)
    mel = []
    prev = nil
    loop do
      if el == [] then break end
      loop do
        if el == [] then break end
        tkn = el.shift
        if tkn.kind_of?(Array) || tkn.kind != TK::SYMBOL then
          prev = tkn
          break
        end
        mel << tkn
      end
      loop do
        if el == [] then break end
        if el[0].kind_of?(Array) then perror "expected operator or ';'" end
        if el[0].kind != TK::SYMBOL then perror "expected operator or ';'" end
        if opl.include? el[0].str then
          prev = [prev, el.shift, el.shift]
        else
          if prev != nil then 
            mel << prev
            prev = nil
          end
          if el != [] then mel << el.shift end
          break
        end
      end
    end
    if prev != nil then mel << prev end
    return mel
  end

これで上手く行くと思うけどあんまり自信はない。

動作テスト

上手く行くかなあ。

~/myc$ myc q12.myc
~/myc$ ./q12
-a[0] = -42
~/myc$

おお、上手く行ったみたいだ。でももうちょっと色々テストした方が良いだろうな。次回はそれだな。

コンパイラ作成(106) バグ修正

バグ

エラー処理周りのチェックをしていてバグを見つけたよ。

// 配列
int main()
{
    double a[10], *p = a;
    a[0) = 42;
    a[1] = 55;
    printf("*p = %f\n", *p);
}

配列の閉じ括弧が間違ってる場合。エラーにならずにコンパイル通っちゃってたよ。

read_el
  # 式の最後までのトークンを読み込む
  #   返り値
  #     el   : expression's token list
  #     kind : 処理しなかったトークン(セミコロンもしくは閉じ括弧)
  #     str  : 処理しなかったトークン(セミコロンもしくは閉じ括弧)
  def read_el(fkind,fstr,skind,sstr)
    el = []
    if fkind == TK::ID && sstr == "(" then
      # 関数呼出の処理
      sel = []
      sel << Token.new(fkind,fstr)
      sel << Token.new(skind,"()")
      skind, sstr = @lex.gettoken
      loop do
        if skind == TK::SYMBOL && sstr == ")" then break end
        fkind, fstr = skind, sstr
        skind, sstr = @lex.gettoken
        pel, skind, sstr = read_modify_el fkind, fstr, skind, sstr
        sel << pel
        if skind == TK::SYMBOL && sstr == "," then
          skind, sstr = @lex.gettoken
        elsif skind != TK::SYMBOL || sstr != ")" then
          perror
        end
      end
      el << sel
      if skind != TK::SYMBOL || sstr != ")" then perror end
      skind, sstr = @lex.gettoken
    elsif fstr == "(" then
      # 括弧の処理
      fkind, fstr = skind, sstr
      skind, sstr = @lex.gettoken
      sel, skind, sstr = read_el fkind, fstr, skind, sstr
      el << sel
      skind, sstr = @lex.gettoken
    elsif sstr == "[" then
      # 配列の処理
      el << Token.new(fkind,fstr)
      el << Token.new(skind,"[]")
      fkind, fstr = @lex.gettoken
      skind, sstr = @lex.gettoken
      sel, skind, sstr = read_el fkind, fstr, skind, sstr
      if sel.size > 1 then
        el << sel
      else
        el << sel[0]
      end
      if skind != TK::SYMBOL || sstr != "]" then perror "expected ']'" end
      skind, sstr = @lex.gettoken
    else
      el << Token.new(fkind,fstr)
    end
    loop do
      if skind == TK::EOF then break end
      if sstr == ";" then break end
      if sstr == ")" then break end
      if sstr == "]" then break end
      if sstr == "," then break end
      if sstr == "}" then break end
      fkind, fstr = skind, sstr
      skind, sstr = @lex.gettoken
      if fkind == TK::ID && sstr == "(" then
        # 関数呼出の処理
        sel = []
        sel << Token.new(fkind,fstr)
        sel << Token.new(skind,"()")
        skind, sstr = @lex.gettoken
        loop do
          if skind == TK::SYMBOL && sstr == ")" then break end
          fkind, fstr = skind, sstr
          skind, sstr = @lex.gettoken
          pel, skind, sstr = read_el fkind, fstr, skind, sstr
          sel << pel
          if skind == TK::SYMBOL && sstr == "," then
            skind, sstr = @lex.gettoken
          elsif skind != TK::SYMBOL || sstr != ")" then
            perror
          end
        end
        el << sel
        if skind != TK::SYMBOL || sstr != ")" then perror end
        skind, sstr = @lex.gettoken
      elsif fstr == "(" then
        # 括弧の処理
        fkind, fstr = skind, sstr
        skind, sstr = @lex.gettoken
        sel, skind, sstr = read_el fkind, fstr, skind, sstr
        el << sel
        skind, sstr = @lex.gettoken
      elsif sstr == "[" then
        # 配列の処理
        el << Token.new(fkind,fstr)
        el << Token.new(skind,"[]")
        fkind, fstr = @lex.gettoken
        skind, sstr = @lex.gettoken
        sel, skind, sstr = read_el fkind, fstr, skind, sstr
        if sel.size > 1 then
          el << sel
        else
          el << sel[0]
        end
        if skind != TK::SYMBOL || sstr != "]" then perror "expected ']'" end
        skind, sstr = @lex.gettoken
      else
        el << Token.new(fkind,fstr)
      end
    end
    return el, skind, sstr
  end

チェックが抜けてたよ。

動作テスト
~/myc$ myc err50.myc
err50.myc:5:8 error: expected ']'
~/myc$

ちゃんとエラーになったよ。次回なにやろうかな。うーん駄目だ。もう頭が働かなくなってる。明日起きてから考えよう。

コンパイラ作成(104) ポインタを配列として扱う

今回の目標

ポインタに[]演算子を適用できるようにするよ。

// ポインタと配列演算子
int main()
{
    char *p = "myc";
    for(int i = 0; i < 3; i = i + 1)
        putchar(p[i]);
    putchar('\n');
}

配列演算子っていう呼び方で合ってるんだろうか。

codegen_els

型チェックの処理を修正。

    # 型チェック
    if type_l == "double" && type_r == "int" then
      if str != "r10d" then
        codegen "  mov    r10d, #{str}"
      end
      codegen "  cvtsi2sd xmm9, r10d"
      str = "xmm9"
      type_r = type_l
    elsif type_l == "int" && type_r == "double" then
      codegen "  cvtsi2sd xmm8, eax"
      type_l = type_r
    end
    if type_l != type_r then
      if is_pointer_type?(type_l) && type_r == "int" then
        if op.str != "+" && op.str != "-" && op.str != "[]" then
          perror "mismatched types to binary operation"
        end
      elsif type_l == "int" && is_pointer_type?(type_r) then
        if op.str != "+" && op.str != "-" && op.str != "[]" then
          perror "mismatched types to binary operation"
        end
      elsif is_array_type?(type_l) && type_r == "int" then
        if op.str != "[]" then
          perror "mismatched types to binary operation"
        end
      elsif type_l == "int" && is_array_type?(type_r) then
        if op.str != "[]" then
          perror "mismatched types to binary operation"
        end
      else
        perror "mismatched types to binary operation"
      end
    elsif is_pointer_type? type_l then
      perror "mismatched types to binary operation"
    elsif type_l == "double" && op.str == "%" then
      perror "mismatched types to binary operation"
    end

ポインタ型のときも[]演算子を弾かないように修正。

      if is_pointer_type?(type_l) && type_r == "int" then
        # ポインタ型+int型の処理
        codegen_pointer_int type_l, op, ostr, str
        if op.str == "[]" then
          codegen_lval_to_rval type_l
        end
      elsif type_l == "int" && is_pointer_type?(type_r) then
        # int型+ポインタ型の処理
        codegen_int_pointer type_r, op, ostr, str
        type_l = type_r
        if op.str == "[]" then
          codegen_lval_to_rval type_l
        end
      elsif is_array_type?(type_l) && type_r == "int" then
        # 配列+int型の処理
        type_l = array_to_pointer type_l
        codegen_pointer_int type_l, op, ostr, str
        type_l = type_l[0,type_l.length-1]
        codegen_lval_to_rval type_l
      elsif type_l == "int" && is_array_type?(type_r) then
        # int型+配列の処理
        type_r = array_to_pointer type_r
        codegen_int_pointer type_r, op, ostr, str
        type_l = type_r[0,type_r.length-1]
        codegen_lval_to_rval type_l
      else
        codegen "  " + ostr + " #{reg}, " + str
      end

codegen_lval_to_rvalをコールするよう修正。

動作テスト
~/myc$ myc q8.myc
~/myc$ ./q8
myc
~/myc$ 

ちゃんと動いたよ。今回は時間が取れなかったんでコーディング2分テスト2分で終わったよ。次回は何にするかな。配列を引数とする関数かな。main(int argc, char *argv[])をコンパイルできるようにしたいな。

コンパイラ作成(103) int型以外のポインタの間接参照

今回の目標

前回に続きバグ修正。

// 配列
int main()
{
    double a[10], *p = a;
    a[0] = 42;
    a[1] = 55;
    printf("*p = %f\n", *p);
}

間接参照演算子もdouble型とかに対応できてなかったよ。

codegen_unaryop

間接参照演算子の処理のところを修正。

    elsif op.str == "*" then
      # 間接参照演算子
      type = codegen_el operand
      if !is_pointer_type? type then
        perror
      end
      type = type[0,type.length-1]
      codegen_lval_to_rval type
=begin
      if is_pointer_type? type then
        codegen "  mov  rax, [rax]"
      else
        codegen "  mov  eax, [rax]"
      end
=end

前回作ったメソッドを呼ぶようにしたよ。

動作テスト
~/myc$ myc q10.myc
~/myc$ ./q10
*p = 42.000000
~/myc$

ちゃんと参照できるようになったよ。二回続けて細かな修正をしたけど、次回は何か機能追加をしたいなあ。ポインタ変数を配列として扱えるようにするかな。

コンパイラ作成(102) int型以外の配列の参照

今回の目標

色々試してたらバグが見つかったよ。

// 配列
int main()
{
    double a[10];
    a[0] = 42;
    a[1] = 55;
    printf("a[0] = %f\n", a[0]);
}

配列の参照だけどint型以外の場合上手く行ってなかった。バグっていうか未サポートってところかな。

codegen_lval_to_rval

新しいメソッドを追加。

  # 左辺値を右辺値に変換するコード生成
  def codegen_lval_to_rval(type)
    if is_pointer_type? type then
      codegen "  mov    rax, [rax]"
    elsif type == "int" then
      codegen "  mov    eax, [rax]"
    elsif type == "char" then
      codegen "  mov    al, [rax]"
    elsif type == "double" then
      codegen "  movsd  xmm8, [rax]"
    else
      perror
    end
  end
codegen_els

配列の処理のところを修正。

      if is_pointer_type?(type_l) && type_r == "int" then
        # ポインタ型+int型の処理
        codegen_pointer_int type_l, op, ostr, str
      elsif type_l == "int" && is_pointer_type?(type_r) then
        # int型+ポインタ型の処理
        codegen_int_pointer type_r, op, ostr, str
        type_l = type_r
      elsif is_array_type?(type_l) && type_r == "int" then
        # 配列+int型の処理
        type_l = array_to_pointer type_l
        codegen_pointer_int type_l, op, ostr, str
        type_l = type_l[0,type_l.length-1]
        codegen_lval_to_rval type_l
      elsif type_l == "int" && is_array_type?(type_r) then
        # int型+配列の処理
        type_r = array_to_pointer type_r
        codegen_int_pointer type_r, op, ostr, str
        type_l = type_r[0,type_r.length-1]
        codegen_lval_to_rval type_l
      else
        codegen "  " + ostr + " #{reg}, " + str
      end

追加したメソッドを呼ぶようにしたよ。

codegen_pointer_int

ここも修正。

  # ポインタ型+int型のコード生成
  def codegen_pointer_int(type_l,op,ostr,str)
    size = sizeof type_l[0,type_l.length-1]
    if str == op.str then
      codegen "  " + ostr + " rax, " + (str.to_i*size).to_s
    elsif str == "r10d" then
      codegen "  movsx r10, r10d"
      if size == 4 then
        codegen "  shl  r10, 2"
        codegen "  " + ostr + " rax, r10"
      elsif size == 8 then
        codegen "  shl  r10, 3"
        codegen "  " + ostr + " rax, r10"
      else
        (0...size).each do
          codegen "  " + ostr + " rax, r10"
        end
      end
    else
      codegen "  mov  r10d, " + str
      codegen "  movsx r10, r10d"
      if size == 4 then
        codegen "  shl  r10, 2"
        codegen "  " + ostr + " rax, r10"
      elsif size == 8 then
        codegen "  shl  r10, 3"
        codegen "  " + ostr + " rax, r10"
      else
        (0...size).each do
          codegen "  " + ostr + " rax, r10"
        end
      end
    end
  end

ここは本当にバグってたよ。size==8のとき2bitしかシフトしてなかった。

動作テスト
~/myc$ myc q9.myc
~/myc$ ./q9
a[0] = 42.000000
~/myc$

ちゃんと動くようになったよ。次回は何にするかな。このところテストが不足してるからまずはテストか。

コンパイラ作成(101) 配列への代入

今回の目標

今回は代入だよ。

// 配列
int main()
{
    int a[10];
    a[0] = 42;
    printf("a[0] = %d\n", a[0]);
}

まずは単純な場合から。

codegen_assign

今回、大きく書き換えたよ。

  # 代入のコード生成
  def codegen_assign(el)
    if el.size != 3 then perror end
    if el[2] == nil then
      perror "broken expression"
    end
    type_r = codegen_el [el[2]]
    if el[0].kind_of?(Array) then
      if el[0][0].str == "*" then
        codegen "  sub  rsp, 8"
        codegen "  push rax"
        type_l = codegen_el [el[0][1]]
        if !is_pointer_type? type_l then
          perror
        end
        type_l = type_l[0,type_l.length-1]
        codegen "  mov  r10, rax"
        codegen "  pop  rax"
        codegen "  add  rsp, 8"
        str = "r10"
      elsif el[0][1].str == "[]" then
        codegen "  sub  rsp, 8"
        codegen "  push rax"
        type_ll = codegen_el [el[0][0]]
        codegen "  sub  rsp, 8"
        codegen "  push rax"
        type_lr = codegen_el [el[0][2]]
        codegen "  mov  r10, rax"
        codegen "  pop  rax"
        codegen "  add  rsp, 8"
        if is_array_type?(type_ll) && type_lr == "int" then
          # 配列+int型の処理
          type_ll = array_to_pointer type_ll
          codegen_pointer_int type_ll, el[0][1], "add ", "r10d"
          type_l = type_ll[0,type_ll.length-1]
        elsif type_l == "int" && is_array_type?(type_r) then
          # int型+配列の処理
          type_lr = array_to_pointer type_lr
          codegen_int_pointer type_lr, el[0][1], "add ", "r10d"
          type_l = type_lr[0,type_lr.length-1]
        else
          perror
        end
        codegen "  mov  r10, rax"
        codegen "  pop  rax"
        codegen "  add  rsp, 8"
        str = "r10"
      else
        perror
      end
    else
      if el[0].kind != TK::ID then perror end
      v = get_var el[0].str
      if v == nil then
        perror "undeclared variable \"" + el[0].str + "\""
      end
      type_l = v[0]
      str = "rbp -  #{v[1]}"
    end
    # 代入
    if type_r == "void*" && is_pointer_type?(type_l) then
      type_r = type_l    # 暗黙の型変換
    elsif type_r == "int" && type_l == "char" then
      type_r = type_l    # 暗黙の型変換
    elsif type_r == "int" && type_l == "double" then
      codegen "  cvtsi2sd xmm8, eax"
      type_r = type_l    # 暗黙の型変換
    elsif is_array_type? type_r then
      type_r = array_to_pointer type_r
    end
    if type_l != type_r then perror end
    if is_pointer_type? type_l then
      codegen "  mov  qword ptr [#{str}], rax"
    elsif type_l == "double"
      codegen "  movsd  qword ptr [#{str}], xmm8"
    elsif type_l == "char"
      codegen "  mov  byte ptr [#{str}], al"
    else
      codegen "  mov  dword ptr [#{str}], eax"
    end
    return type_l
  end

同じような処理を二箇所に書いてたんだけど、それだと色々修正していく上で問題になるんで、一個に纏めたよ。本題の配列への対応だけど、左辺値を求める処理を組み込んだよ。ただ、この方法で良いかは正直良く分かんないな。多次元配列とかに対応できるか不安だよ。もっと真面目に左辺値を求めるようにしないと駄目かも。この辺要検討だな。

動作テスト
~/myc$ myc q6.myc
~/myc$ ./q6
a[0] = 42
~/myc$

大丈夫かな。そういやテストプログラムの配列の添え字、いつも0だなあ。違うのもやってみるか。

// エラトステネスの篩
int main()
{
    int a[100];
    for(int i = 0; i < 100; i = i + 1)
        a[i] = 1;
    for(int i = 2; i < 100; i = i + 1) {
        for(int j = i * 2; j < 100; j = j + i) {
            a[j] = 0;
        }
    }
    for(int i = 2; i < 100; i = i + 1)
        if( a[i] == 1 ) printf("%d ",i);
    printf("\n");
}

ちょろちょろっと書いたプログラムなんで効率悪いけど、配列のテストにはなるよね。

~/myc$ myc q7.myc
~/myc$ ./q7
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 
~/myc$

おお、ちゃんと素数が表示されてる。代入は参照より楽だったよ。もうちょっと苦労するかと思ったんだけどね。これで配列のサポートは大方できたかな。次回以降残ったとことエラー処理周りをやってくよ。

コンパイラ作成(100) 配列の参照

今回の目標

今回は配列の参照だよ。

// 配列
int main()
{
    int a[10], *p;
    p = a;
    *p = 42;
    printf("a[0] = %d\n", a[0]);
    printf("0[a] = %d\n", 0[a]);
    printf("10 + a[0] = %d\n", 10 + a[0]);
    printf("10 + 0[a] = %d\n", 10 + 0[a]);
}

配列への代入は今回はやらないんでポインタを通して値を設定してる。

read_el

今回は修正箇所多いよ。まずは式リストの読み込み処理。

  # 式の最後までのトークンを読み込む
  #   返り値
  #     el   : expression's token list
  #     kind : 処理しなかったトークン(セミコロンもしくは閉じ括弧)
  #     str  : 処理しなかったトークン(セミコロンもしくは閉じ括弧)
  def read_el(fkind,fstr,skind,sstr)
    el = []
    if fkind == TK::ID && sstr == "(" then
      # 関数呼出の処理
      sel = []
      sel << Token.new(fkind,fstr)
      sel << Token.new(skind,"()")
      skind, sstr = @lex.gettoken
      loop do
        if skind == TK::SYMBOL && sstr == ")" then break end
        fkind, fstr = skind, sstr
        skind, sstr = @lex.gettoken
        pel, skind, sstr = read_modify_el fkind, fstr, skind, sstr
        sel << pel
        if skind == TK::SYMBOL && sstr == "," then
          skind, sstr = @lex.gettoken
        elsif skind != TK::SYMBOL || sstr != ")" then
          perror
        end
      end
      el << sel
      if skind != TK::SYMBOL || sstr != ")" then perror end
      skind, sstr = @lex.gettoken
    elsif fstr == "(" then
      # 括弧の処理
      fkind, fstr = skind, sstr
      skind, sstr = @lex.gettoken
      sel, skind, sstr = read_el fkind, fstr, skind, sstr
      el << sel
      skind, sstr = @lex.gettoken
    elsif sstr == "[" then
      # 配列の処理
      el << Token.new(fkind,fstr)
      el << Token.new(skind,"[]")
      fkind, fstr = @lex.gettoken
      skind, sstr = @lex.gettoken
      sel, skind, sstr = read_el fkind, fstr, skind, sstr
      if sel.size > 1 then
        el << sel
      else
        el << sel[0]
      end
      skind, sstr = @lex.gettoken
    else
      el << Token.new(fkind,fstr)
    end
    loop do
      if skind == TK::EOF then break end
      if sstr == ";" then break end
      if sstr == ")" then break end
      if sstr == "]" then break end
      if sstr == "," then break end
      if sstr == "}" then break end
      fkind, fstr = skind, sstr
      skind, sstr = @lex.gettoken
      if fkind == TK::ID && sstr == "(" then
        # 関数呼出の処理
        sel = []
        sel << Token.new(fkind,fstr)
        sel << Token.new(skind,"()")
        skind, sstr = @lex.gettoken
        loop do
          if skind == TK::SYMBOL && sstr == ")" then break end
          fkind, fstr = skind, sstr
          skind, sstr = @lex.gettoken
          pel, skind, sstr = read_el fkind, fstr, skind, sstr
          sel << pel
          if skind == TK::SYMBOL && sstr == "," then
            skind, sstr = @lex.gettoken
          elsif skind != TK::SYMBOL || sstr != ")" then
            perror
          end
        end
        el << sel
        if skind != TK::SYMBOL || sstr != ")" then perror end
        skind, sstr = @lex.gettoken
      elsif fstr == "(" then
        # 括弧の処理
        fkind, fstr = skind, sstr
        skind, sstr = @lex.gettoken
        sel, skind, sstr = read_el fkind, fstr, skind, sstr
        el << sel
        skind, sstr = @lex.gettoken
      elsif sstr == "[" then
        # 配列の処理
        el << Token.new(fkind,fstr)
        el << Token.new(skind,"[]")
        fkind, fstr = @lex.gettoken
        skind, sstr = @lex.gettoken
        sel, skind, sstr = read_el fkind, fstr, skind, sstr
        if sel.size > 1 then
          el << sel
        else
          el << sel[0]
        end
        skind, sstr = @lex.gettoken
      else
        el << Token.new(fkind,fstr)
      end
    end
    return el, skind, sstr
  end

配列をa[0]から(a[]0)へと変形してる。こうすることで二項演算子と同じになるんで後の処理が楽になるよ。今回の修正ではエラー処理とかすっ飛ばしちゃってるよ。とりあえず正常なソースをコンパイルできるところまで何とかするけど、それ以外はあちこち手抜きだよ。

read_modify_el

次はここ。

  # 式の最後までのトークンを読み込み、変形する
  def read_modify_el(fkind,fstr,skind,sstr)
    el, kind, str = read_el fkind, fstr, skind, sstr
    puts to_str(el) if $opt_d   # デバッグ用
    el = modify_el_unaryop el, ["+","-","&","*"]
    el = modify_el el, ["[]"]
    el = modify_el el, ["*","/","%"]
    el = modify_el el, ["+","-"]
    el = modify_el el, ["<",">","<=",">="]
    el = modify_el el, ["==","!="]
    el = modify_el el, ["="], :r_to_l
    puts to_str(el) if $opt_d   # デバッグ用
    return el, kind, str
  end

[]を追加したんだけどこれ問題ありだよ。C言語の仕様では配列は単項演算子より優先順位が上なんで、本当はel = modify_el el, ["[]"]をel = modify_el_unaryop el, ["+","-","&","*"]より前に持っていかなきゃならないんだけど、modify_elは単項演算子が混じってるelに対応できるようになってないんだよね。それで今回はこういう順番にしちゃった。配列のサポートがある程度出来上がったら修正しようと思ってるよ。

新しいメソッド

codegen_elsから処理を切り出したよ。

  # ポインタ型+int型のコード生成
  def codegen_pointer_int(type_l,op,ostr,str)
    size = sizeof type_l[0,type_l.length-1]
    if str == op.str then
      codegen "  " + ostr + " rax, " + (str.to_i*size).to_s
    elsif str == "r10d" then
      codegen "  movsx r10, r10d"
      if size == 4 then
        codegen "  shl  r10, 2"
        codegen "  " + ostr + " rax, r10"
      elsif size == 8 then
        codegen "  shl  r10, 2"
        codegen "  " + ostr + " rax, r10"
      else
        (0...size).each do
          codegen "  " + ostr + " rax, r10"
        end
      end
    else
      codegen "  mov  r10d, " + str
      codegen "  movsx r10, r10d"
      if size == 4 then
        codegen "  shl  r10, 2"
        codegen "  " + ostr + " rax, r10"
      elsif size == 8 then
        codegen "  shl  r10, 3"
        codegen "  " + ostr + " rax, r10"
      else
        (0...size).each do
          codegen "  " + ostr + " rax, r10"
        end
      end
    end
  end

  # int型+ポインタ型のコード生成
  def codegen_int_pointer(type_r,op,ostr,str)
    size = sizeof type_r[0,type_r.length-1]
    codegen "  movsx rax, eax"
    if size == 1 then
    elsif size == 4 then
      codegen "  shl  rax, 2"
    elsif size == 8 then
      codegen "  shl  rax, 3"
    else
      codegen "  mov  r11, rax"
      (0...size-1).each do
        codegen "  add  rax, r11"
      end
    end
    codegen "  " + ostr + " rax, " + str
  end

配列の参照処理はポインタとint型の加算と同じような処理なんで切り出したメソッドを活用してくよ。

mnemonic
  # ニーモニック
  def mnemonic(op, type)
    if type == "double" then
      if op.str == "+" then
        return "addsd "
      elsif op.str == "-" then
        return "subsd "
      elsif op.str == "*" then
        return "mulsd "
      elsif op.str == "/" then
        return "divsd "
      elsif op.str == "%" then
        return "divsd "
      elsif op.str == "==" then
        return "ucomisd"
      elsif op.str == "!=" then
        return "ucomisd"
      elsif op.str == "<" || op.str == "<" || op.str == ">" || op.str == "<=" || op.str == ">=" then
        return "ucomisd"
      elsif op.str == "[]" then
        return "addsd "
      else
        perror "unknown operator \"" + op.str + "\""
      end
    else
      if op.str == "+" then
        return "add "
      elsif op.str == "-" then
        return "sub "
      elsif op.str == "*" then
        return "imul"
      elsif op.str == "/" then
        return "idiv"
      elsif op.str == "%" then
        return "idiv"
      elsif op.str == "==" then
        return "cmp "
      elsif op.str == "!=" then
        return "cmp "
      elsif op.str == "<" || op.str == "<" || op.str == ">" || op.str == "<=" || op.str == ">=" then
        return "cmp "
      elsif op.str == "[]" then
        return "add "
      else
        perror "unknown operator \"" + op.str + "\""
      end
    end
  end

ここは[]を追加しただけ。

codegen_els

コード生成部。あちこちに手を入れたよ。

  # 式のコード生成(二項演算の右側被演算子)
  def codegen_els(op, operand, type_l)
    # 右被演算子を評価
    type_r = "int"
    if operand.kind_of?(Array) then
      if operand[0].size == 2 && operand[0].kind == TK::ID && operand[1].str == "()" then
        if type_l == "double" then
          codegen "  sub    rsp, 16"
          codegen "  movsd  [rsp], xmm8"
          type_r = codegen_func operand
          if type_r == "double" then
            codegen "  movsd  xmm9, xmm8"
            str = "xmm9"
          else
            codegen "  mov  r10d, eax"
            str = "r10d"
          end
          codegen "  movsd  xmm8, [rsp]"
          codegen "  add    rsp, 16"
        else
          codegen "  sub  rsp, 8"
          codegen "  push rax"
          type_r = codegen_func operand
          if type_r == "double" then
            codegen "  movsd  xmm9, xmm8"
            str = "xmm9"
          else
            codegen "  mov  r10d, eax"
            str = "r10d"
          end
          codegen "  pop  rax"
          codegen "  add  rsp, 8"
        end
      else
        if type_l == "double" then
          codegen "  sub    rsp, 16"
          codegen "  movsd  [rsp], xmm8"
          type_r = codegen_el operand
          if type_r == "double" then
            codegen "  movsd  xmm9, xmm8"
            str = "xmm9"
          else
            codegen "  mov  r10d, eax"
            str = "r10d"
          end
          codegen "  movsd  xmm8, [rsp]"
          codegen "  add    rsp, 16"
        else
          codegen "  sub  rsp, 8"
          codegen "  push rax"
          type_r = codegen_el operand
          if type_r == "double" then
            codegen "  movsd  xmm9, xmm8"
            str = "xmm9"
          else
            codegen "  mov  r10d, eax"
            str = "r10d"
          end
          codegen "  pop  rax"
          codegen "  add  rsp, 8"
        end
      end
    elsif operand.kind == TK::ID then
      v = get_var operand.str
      if v == nil then
        perror "undeclared variable \"" + operand.str + "\""
      end
      type_r = v[0]
      if is_array_type? type_r then
        codegen "  lea  r10, [rbp - " + v[1].to_s + "]"
        str = "r10"
      elsif is_pointer_type? type_r then
        str = "qword ptr [rbp - " + v[1].to_s + "]"
      elsif type_r == "double" then
        str = "qword ptr [rbp - " + v[1].to_s + "]"
      elsif type_r == "char" then
        str = "byte ptr [rbp - " + v[1].to_s + "]"
      else
        str = "dword ptr [rbp - " + v[1].to_s + "]"
      end
    elsif operand.kind == TK::NUMBER then
      str = operand.str
    elsif operand.kind == TK::STRING then
      type_r = "char*"
      label = addliteral operand.str
      codegen "  lea  r10, "+label
      str = "r10"
    elsif operand.kind == TK::FLOAT then
      # 浮動小数点数リテラル
      type_r = "double"
      label = addliteral operand.str, :double
      str = "qword ptr [#{label}]"
    else
      perror
    end

    # 型チェック
    if type_l == "double" && type_r == "int" then
      if str != "r10d" then
        codegen "  mov    r10d, #{str}"
      end
      codegen "  cvtsi2sd xmm9, r10d"
      str = "xmm9"
      type_r = type_l
    elsif type_l == "int" && type_r == "double" then
      codegen "  cvtsi2sd xmm8, eax"
      type_l = type_r
    end
    if type_l != type_r then
      if is_pointer_type?(type_l) && type_r == "int" then
        if op.str != "+" && op.str != "-" then
          perror "mismatched types to binary operation"
        end
      elsif type_l == "int" && is_pointer_type?(type_r) then
        if op.str != "+" && op.str != "-" then
          perror "mismatched types to binary operation"
        end
      elsif is_array_type?(type_l) && type_r == "int" then
        if op.str != "[]" then
          perror "mismatched types to binary operation"
        end
      elsif type_l == "int" && is_array_type?(type_r) then
        if op.str != "[]" then
          perror "mismatched types to binary operation"
        end
      else
        perror "mismatched types to binary operation"
      end
    elsif is_pointer_type? type_l then
      perror "mismatched types to binary operation"
    elsif type_l == "double" && op.str == "%" then
      perror "mismatched types to binary operation"
    end

    reg = "eax"
    if type_l == "double" then
      reg = "xmm8"
    elsif type_l == "char" then
      reg = "al"
    end

    # 左被演算子と右被演算子とで計算
    ostr = mnemonic op, type_l

    if op.str == "==" then
      codegen "  " + ostr + " #{reg}, " + str
      if type_l == "double" then
        codegen "  sete  al"
        codegen "  setnp r10b"
        codegen "  and   al, r10b"
        codegen "  and   eax, 1"
        type_l = "int"
      else
        codegen "  sete al"
        codegen "  and  eax, 1"
      end
    elsif op.str == "!=" then
      codegen "  " + ostr + " #{reg}, " + str
      if type_l == "double" then
        codegen "  setne al"
        codegen "  setp  r10b"
        codegen "  or    al, r10b"
        codegen "  and   eax, 1"
        type_l = "int"
      else
        codegen "  setne al"
        codegen "  and  eax, 1"
      end
    elsif op.str == "<" then
      codegen "  " + ostr + " #{reg}, " + str
      if type_l == "double" then
        codegen "  setb  al"
        codegen "  and   eax, 1"
        type_l = "int"
      else
        codegen "  setl al"
        codegen "  and  eax, 1"
      end
    elsif op.str == ">" then
      codegen "  " + ostr + " #{reg}, " + str
      if type_l == "double" then
        codegen "  seta  al"
        codegen "  and   eax, 1"
        type_l = "int"
      else
        codegen "  setg al"
        codegen "  and  eax, 1"
      end
    elsif op.str == "<=" then
      codegen "  " + ostr + " #{reg}, " + str
      if type_l == "double" then
        codegen "  setbe al"
        codegen "  and   eax, 1"
        type_l = "int"
      else
        codegen "  setle al"
        codegen "  and  eax, 1"
      end
    elsif op.str == ">=" then
      codegen "  " + ostr + " #{reg}, " + str
      if type_l == "double" then
        codegen "  setae al"
        codegen "  and   eax, 1"
        type_l = "int"
      else
        codegen "  setge al"
        codegen "  and  eax, 1"
      end
    elsif type_l != "double" && (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"
    else
      if is_pointer_type?(type_l) && type_r == "int" then
        # ポインタ型+int型の処理
        codegen_pointer_int type_l, op, ostr, str
      elsif type_l == "int" && is_pointer_type?(type_r) then
        # int型+ポインタ型の処理
        codegen_int_pointer type_r, op, ostr, str
        type_l = type_r
      elsif is_array_type?(type_l) && type_r == "int" then
        # ポインタ型+int型の処理
        type_l = array_to_pointer type_l
        codegen_pointer_int type_l, op, ostr, str
        codegen "  mov  eax, [rax]"
        type_l = type_l[0,type_l.length-1]
      elsif type_l == "int" && is_array_type?(type_r) then
        type_r = array_to_pointer type_r
        codegen_int_pointer type_r, op, ostr, str
        codegen "  mov  eax, [rax]"
        type_l = type_r[0,type_r.length-1]
      else
        codegen "  " + ostr + " #{reg}, " + str
       end
    end
    return type_l
  end

これで全部修正できたかな。

動作テスト
~/myc$ myc q4.myc
~/myc$ ./q4
a[0] = 42
0[a] = 42
10 + a[0] = 52
10 + 0[a] = 52
~/myc$ 

おお、動いたよ。今回は修正箇所多くて大変だったけどなんとか上手く行った。さて次は何やろう。配列への代入かな。