Concurrent Clean : ABCマシン(4)

[id:lethevert:20051226:p2]
昨日挙げた p.396(in Ch12.Concret.ABC.ps)は、ABCコードを実現化したMotorola codeでした。
ABCコードの例は、p.350(in Ch11.Ch11.Cl_to_ABC.ps)以降にいくつか載っています。

      • -

Appendix Cには、Cleanに近いコードで、ABCマシンの実装コードが記述されています。これを、そのままCleanでインプリメントすれば、ABCマシンになるんじゃないか?

      • -

Lengthの例を参考に、手元のコンパイラコンパイルしてみました。

Length :: !Int ![x] -> Int
Length n [a:b] = Length (n+1) b
Length n [] = n

(+) :: !Int !Int -> Int
(+) a b
	= code inline {
		addI
	}

このCleanコードが下のABCコードにコンパイルされます。・・・かなり雰囲気が違うんですが。

.comp 919 000111010
.code       0       0       0
.start _nostart_
.depend "length" "20051227084520"
.module m_length "length" "20051227084628"
.endinfo
.implab _cycle_in_spine
.implab _type_error
.implab _hnf
.impdesc _ind
.implab _indirection _eaind
.impdesc e_system_dif
.implab e_system_nif e_system_eaif
.implab e_system_sif
.impdesc e_system_dAP
.implab e_system_nAP e_system_eaAP
.implab e_system_sAP
.impdesc _Nil
.impdesc _Cons
.impdesc _Conss
.implab n_Conss ea_Conss
.impdesc _Consts
.implab n_Consts ea_Consts
.impdesc _Conssts
.implab n_Conssts ea_Conssts
.impdesc _Tuple
.impdesc d_S.1
.implab n_S.1 ea_S.1
.impdesc d_S.2
.implab n_S.2 ea_S.2
.impdesc d_S.3
.implab n_S.3 ea_S.3
.impdesc d_S.4
.implab n_S.4 ea_S.4
.impdesc d_S.5
.implab n_S.5 ea_S.5
.impdesc d_S.6
.implab n_S.6 ea_S.6
.implab _driver
.export e_length_sLength
.descexp e_length_dLength e_length_nLength e_length_lLength 2 0 "Length"
.o 2 0
e_length_lLength
	repl_args 1 1
.d 2 0
	jsr ea1
.o 0 1 i
	buildI_b 0
	pop_b 1
.d 1 0
	rtn
.n 2 e_length_dLength
.o 1 0
e_length_nLength
	push_node _cycle_in_spine 2
.d 2 0
	jsr ea1
.o 0 1 i
	fillI_b 0 0
	pop_b 1
.d 1 0
	rtn
.o 2 0
ea1
	jsr_eval 1
	jsr_eval 0
	pushI_a 0
	pop_a 1
.o 1 1 i
e_length_sLength
.o 1 1 i
s1
	eq_desc _Cons 2 0
	jmp_true case.1
	jmp case.2
case.1
	repl_args 2 2
	pop_a 1
	push_a 0
	jsr_eval 0
	pushI 1
	push_b 1
	update_b 1 2
	updatepop_b 0 1
	buildh _Nil 0
	update_a 0 2
	pop_a 1
	addI

	updatepop_a 0 1
.d 1 1 i
	jmp s1
case.2
	pop_a 1
.d 0 1 i
	rtn
.o 0 2 ii
s2
.inline +
	addI
.end

.d 0 1 i
	rtn

「.」で始まる行と空行を取り除いたのが、下。「.」で始まる行を取り除いたのは、特に理由はなく、勘で、取り除いても意味が通りそうと思ったから。

e_length_lLength
	repl_args 1 1
	jsr ea1
	buildI_b 0
	pop_b 1
	rtn
e_length_nLength
	push_node _cycle_in_spine 2
	jsr ea1
	fillI_b 0 0
	pop_b 1
	rtn
ea1
	jsr_eval 1
	jsr_eval 0
	pushI_a 0
	pop_a 1
e_length_sLength
s1
	eq_desc _Cons 2 0
	jmp_true case.1
	jmp case.2
case.1
	repl_args 2 2
	pop_a 1
	push_a 0
	jsr_eval 0
	pushI 1
	push_b 1
	update_b 1 2
	updatepop_b 0 1
	buildh _Nil 0
	update_a 0 2
	pop_a 1
	addI
	updatepop_a 0 1
	jmp s1
case.2
	pop_a 1
	rtn
s2
	addI
	rtn