ãã¡ã€ã«åã®é·ãã¯ç¡éã§ãããç¡éã¯255æåã«èšå®ãããŸãã
-ããŒã¿ãŒã³ãªã³ãœã³ïŒUnixãã¡ã€ã«ã·ã¹ãã
ãããã£ãŠãp-codeã®ããã°ã©ã ããããç¡å¶éã®æ°ã®ã¬ãžã¹ã¿ïŒã€ãŸã255ïŒãèªç±ã«äœ¿çšã§ããŸãã å®éã®ããã»ããµã®ã¬ãžã¹ã¿ã®æ°ã¯ã¯ããã«å°ãªãïŒ4ã€ãšä»®å®ïŒã ã©ãããïŒ
ããã«æçš¿ïŒ
- Pã³ãŒã解æ
- ã©ã€ãã¿ã€ã
- å®è£
- ã·ã³ãã«ãªæé©å
- ããŒãžã§ã³åå²
- ã¡ã¢ãªãæäœãã
- ã©ãããã®ïŒ
Pã³ãŒã解æ
æåŸã®æçš¿ã§ã¯ãã³ã³ãã€ã«ãããããã°ã©ã ã®ãã³ãããããŸããã 40ã®ããŒã ã¯ç°¡åã«è§£èªã§ããŸãã
00 mov r01,0
01 mov r02ã0x3e8
02ãšã³ãŒ0x126
03 echo r01
04ãšã³ãŒ0xa0
05ãšã³ãŒr02
06ãšã³ãŒ0xa7
07 le r03ãr01ãr02
08 jz r03ã+ 0x1dïŒ= 0x26ïŒ
09 r04ãr01ãr02ãè¿œå
0a mov r05ã2
0b div r06ãr04ãr05
0cãšã³ãŒ0x162
0dãšã³ãŒr06
0eãšã³ãŒ0xcc
0få
¥år07
10 mov r08ã1
11 eq r09ãr07ãr08
12 jz r09ã+ 4ïŒ= 0x17ïŒ
13 mov r0aã1
14ãµãr0bãr06ãr0a
15 r02ãr0bã0ãè¿œå
16 jz 0ã+ eïŒ= 0x25ïŒ
17 mov r0cã2
18 eq r0dãr07ãr0c
19 jz r0dã+ 4ïŒ= 0x1eïŒ
1a mov r0eã1
1b r0fãr06ãr0eãè¿œå
1c r01ãr0fã0ãè¿œå
1d jz 0ã+ 7ïŒ= 0x25ïŒ
1e mov r10ã3
1f eq r11ãr07ãr10
20 jz r11ã+ 3ïŒ= 0x24ïŒ
21ãšã³ãŒ0x146
22 hlt
23 jz 0ã+ 1ïŒ= 0x25ïŒ
24ãšã³ãŒ0x16a
25 jz 0ã-0x1fïŒ= 7ïŒ
26ãšã³ãŒ0xff
27 hlt
ãã¹ãŠã®ã¬ãžã¹ã¿ãåæã«äœ¿çšãããããã§ã¯ãªãããšãããããŸããã»ãšãã©ã®ã¬ãžã¹ã¿ã¯ã次ã®ã³ãã³ãã«ã®ã¿å¿
èŠãªäžéå€ãä¿åããŸãã ããšãã°ã
r03
ã³ãã³ã
07-08
ã§ã®ã¿äœ¿çšããã
r03
ããŒã
1b-1c
ã®ã¿äœ¿çšãããŸãã ããã¯ãæ ŒçŽãããå€ã«åãç©çã¬ãžã¹ã¿ã䜿çšããããšã«é害ããªãããšãæå³ããŸã
07-08
ã³ãã³ãã§ã¯1ã€ã®å€ã«äœ¿çšããã
1b-1c
ã§ã¯å¥ã®å€ã«äœ¿çšãããŸãã
ç°ãªãå€ã«åæã«äœ¿çšã
ããªã2ã€ã®ã¬ãžã¹ã¿ã¯ã1ã€ã®ç©çã¬ãžã¹ã¿ã«çµåã§ããŸãã
ãç°ãªãå€ã«åæã«äœ¿çšããããã®å®çŸ©ã«ã¯ããããªåŸ®åŠãããããŸãã 第äžã«ãã¬ãžã¹ã¿ã®å¯¿åœãæåã®èšåããæåŸã®èšåãŸã§ã®åãªãééãšèŠãªãããšã¯äžå¯èœã§ããã³ãŒãã«ã¯ãããšãã°ããã¹ãŠã®æ¹åã®ãžã£ã³ããå«ããããšãã§ããŸãã
...
10 mov rã42
11 jz 0ã20
12ãšã³ãŒr
...
20 ...
...
30 jz 0ã12
...
r
ã¯ããŒã 10ã12ã§ã®ã¿èšåãããŠããŸãããããŒã 20ã§ãæé»çã«äœ¿çšãããŸããããã¯ããã®åŸã12ã«æ»ãããšãã§ããããã§ãã ãããã£ãŠã
r
ã¯ã³ãã³ã20ã§äœ¿çšãããä»ã®ã¬ãžã¹ã¿ãšçµã¿åãããããšã¯ã§ããŸããã
第äºã®åŸ®åŠãïŒ
...
10 mov rã42
11ãšã³ãŒr
...
20 ...
...
30 mov rã24
31ãšã³ãŒr
...
ã³ãã³ã20ã§ã¯ãã¬ãžã¹ã¿
r
ã¯å®éã«ã¯äœ¿çšãããŠããŸãããããã®åïŒ10-11ïŒãšãã®åŸïŒ30-31ïŒã®äž¡æ¹ã«èšåãããŠããŸãã 次ã®äœ¿çšïŒ31ïŒã®åã«å€ãæ°ããæ¹æ³ã§å²ãåœãŠããããããä»ã®å€ãä¿åã§ããŸãã åºæ¬çã«ãå€ãç°ãªãããã10-11ãš31-31ã§
r
ã«ç°ãªãç©çã¬ãžã¹ã¿ãå²ãåœãŠãããšãã§ããŸãã ãã ãããã®ããã«ã¯ã
r
2ã€ã®ç¬ç«ãããããŒãžã§ã³ã
r
ããšã確èªããããããåå¥ã«åŠçããå¿
èŠããããŸãã
ã©ã€ãã¿ã€ã
ã¬ãžã¹ã¿ã䜿çšãããã³ãã³ãã®ã»ãããç¹°ãè¿ã決å®ããããšãã§ããŸãã
- ã¬ãžã¹ã¿ã¯ããã®å€ãèªã¿åãããŒã ã«ãã£ãŠå¿
èŠãšãããŸãã
- ããŒã AããBã«ïŒçŽæ¥ãŸãã¯ãžã£ã³ãã§ïŒè¡ãããšãã§ããBãã¬ãžã¹ã¿ãå¿
èŠãšããå ŽåãAããããå¿
èŠãšããŸãã
- registerã¯ãå€ãèšå®ããã³ãã³ãã«å¿
èŠãããŸããã
åã³ãã³ãã«ã€ããŠããå
¥åæãããã³ãåºåæãã«å¿
èŠãªã¬ãžã¹ã¿ã®ã»ããã決å®ãããããã®ã»ãããã€ã³ã¹ããŒã«ããããŸã§3ã€ã®ã«ãŒã«ãé©çšããŸãã ããã°ã©ã ã§åŸããããã®ã¯æ¬¡ã®ãšããã§ãã
å埩æ°ïŒ1ïŒå
¥å/åºåïŒ2ïŒå
¥å/åºåïŒ3ïŒå
¥å/åºåïŒ4ïŒå
¥å/åºåïŒ...åèšïŒå
¥å/åºåïŒ
00 mov r01,0 / r01
01 mov r02ã0x3e8 / r01 r01 / r01ãr02
02 echo 0x126 / r01 r01 / r01 r01 / r01 r01ãr02 / r01ãr02
03 echo r01 r01 / r01 / r01 / r01 / r02 r01ãr02 / r01ãr02
04 echo 0xa0 / r02 r02 / r02 r02 / r02 r01ãr02 / r01ãr02
05 echo r02 r02 / r02 / r02 / r02 / r01ãr02 r01ãr02 / r01ãr02
06 echo 0xa7 / r01ãr02 r01ãr02 / r01ãr02 r01ãr02 / r01ãr02 r01ãr02 / r01ãr02
07 le r03ãr01ãr02 r01ãr02 / r01ãr02 / r03 r01ãr02 / r03 r01ãr02 / r01ãr02ãr03 r01ãr02 / r01ãr02ãr03
08 jz r03ã+ 0x1dïŒ= 0x26ïŒr03 / r03 / r01ãr02 r01ãr02ãr03 / r01ãr02 r01ãr02ãr03 / r01ãr02 r01ãr02ãr03 / r01ãr02
09 r04ãr01ãr02 r01ãr02 / r01ãr02 / r01ãr02 / r01ãr02 / r04 r01ãr02 / r01ãr02ãr04ãè¿œå
0a mov r05ã2 / r04ãr05 r04 / r04ãr05 r04 / r04ãr05 r01ãr02ãr04 / r01ãr02ãr04ãr05
0b div r06ãr04ãr05 r04ãr05 / r04ãr05 / r04ãr05 / r04ãr05 / r06 r01ãr02ãr04ãr05 / r01ãr02ãr06
0cãšã³ãŒ0x162 / r06 r06 / r06 r06 / r06 r01ãr02ãr06 / r01ãr02ãr06
0dãšã³ãŒr06 r06 / r06 / r06 / r06 / r01ãr02ãr06 / r01ãr02ãr06
0eãšã³ãŒ0xcc r01ãr02ãr06 / r01ãr02ãr06
0få
¥år07 / r07 r01ãr02ãr06 / r01ãr02ãr06ãr07
10 mov r08ã1 / r07ãr08 r07 / r07ãr08 r07 / r07ãr08 r01ãr02ãr06ãr07 / r01ãr02ãr06ãr07ãr08
11 eq r09ãr07ãr08 r07ãr08 / r07ãr08 / r09 r07ãr08 / r09 r07ãr08 / r09 r01ãr02ãr06ãr07ãr08 / r01ãr02ãr06ãr07ãr09
12 jz r09ã+ 4ïŒ= 0x17ïŒr09 / r09 / r09 / r09 / r06ãr07 r01ãr02ãr06ãr07ãr09 / r01ãr02ãr06ãr07
13 mov r0aã1 / r06ãr0a r06 / r06ãr0a r06 / r06ãr0a r01ãr06 / r01ãr06ãr0a
14ãµãr0bãr06ãr0a r06ãr0a / r06ãr0a / r0b r06ãr0a / r0b r06ãr0a / r0b r01ãr06ãr0a / r01ãr0b
15 r02ãr0bã0 r0b / r0b / r0b / r0b / r01ãr0b / r01ãr02ãè¿œå
16 jz 0ã+ eïŒ= 0x25ïŒr01ãr02 / r01ãr02
17 mov r0cã2 / r07ãr0c r07 / r07ãr0c r07 / r07ãr0c r01ãr02ãr06ãr07 / r01ãr02ãr06ãr07ãr0c
18 eq r0dãr07ãr0c r07ãr0c / r07ãr0c / r0d r07ãr0c / r0d r07ãr0c / r0d r01ãr02ãr06ãr07ãr0c / r01ãr02ãr06ãr07ãr0d
19 jz r0dã+ 4ïŒ= 0x1eïŒr0d / r0d / r0d / r0d / r06ãr07 r01ãr02ãr06ãr07ãr0d / r01ãr02ãr06ãr07
1a mov r0eã1 / r06ãr0e r06 / r06ãr0e r06 / r06ãr0e r02ãr06 / r02ãr06ãr0e
1b r0fãr06ãr0e r06ãr0e / r06ãr0e / r0f r06ãr0e / r0f r06ãr0e / r0f r02ãr06ãr0e / r02ãr0fãè¿œå
1c r01ãr0fã0 r0f / r0f / r0f / r0f / r02ãr0f / r01ãr02ãè¿œå
1d jz 0ã+ 7ïŒ= 0x25ïŒ/ r01ãr02 r01ãr02 / r01ãr02
1e mov r10ã3 / r07ãr10 r07 / r07ãr10 r07 / r07ãr10 r01ãr02ãr07 / r01ãr02ãr07ãr10
1f eq r11ãr07ãr10 r07ãr10 / r07ãr10 / r11 r07ãr10 / r11 r07ãr10 / r11 r01ãr02ãr07ãr10 / r01ãr02ãr11
20 jz r11ã+ 3ïŒ= 0x24ïŒr11 / r11 / r11 / r11 / r01ãr02ãr11 / r01ãr02
21ãšã³ãŒ0x146
22 hlt
23 jz 0ã+ 1ïŒ= 0x25ïŒ/ r01ãr02 r01ãr02 / r01ãr02
24ãšã³ãŒ0x16a / r01ãr02 r01ãr02 / r01ãr02
25 jz 0ã-0x1fïŒ= 7ïŒ/ r01ãr02 r01ãr02 / r01ãr02 r01ãr02 / r01ãr02 r01ãr02 / r01ãr02
26ãšã³ãŒ0xff
27 hlt
åã
ã®å埩ã«ã€ããŠïŒ
- åã³ãã³ãã®å
¥åæ-ã³ãã³ãã䜿çšããã¬ãžã¹ã¿ã ïŒã«ãŒã«çªå·1ïŒ
- åã³ãã³ãã®åºåã§ãããããå°éå¯èœãªã³ãã³ãã®å
¥åãã³ããŒããŸãã ïŒã«ãŒã«çªå·2ïŒ
- åã³ãã³ãã®å
¥åã§ãã³ãã³ãã«ãã£ãŠèšé²ãããã¬ãžã¹ã¿ãé€ããŠããã®åºåãã³ããŒããŸãã ïŒã«ãŒã«çªå·3ïŒ
- åã³ãã³ãã®åºåã§ãããããå°éå¯èœãªã³ãã³ãã®å
¥åãã³ããŒããŸãã ïŒã«ãŒã«çªå·2ïŒ
ããã«ãã«ãŒã«No. 2ãšNo. 3ã亀äºã«ç¹°ãè¿ãããŸãã
æ°åã®å埩ã®åŸããã¬ãžã¹ã¿ã®åç¶å¯èœæ§ãã®æçµããŒã¯ã¢ãããååŸããŸããããã¯ãåããŒã ã®ããŒãºãç»é²ããŸãã 次ã«ãpã³ãŒãã®ã¬ãžã¹ã¿ã®ååãç©ççãªãã®ã«å€æŽã§ããŸããããšãã°ãã貪欲ãã§ãããæãå¿
èŠãªãã¬ãžã¹ã¿ïŒã³ãã³ãã®æ倧æ°ãå¿
èŠïŒãååŸããŸãã 察å¿ããããŒã ã§ãŸã 䜿çšãããŠããªãç©ççãªååã«å€æŽããŸãã ç¹°ãè¿ããŸãã
å¯èœãªéãååãªç©çã¬ãžã¹ã¿ãŒãæäŸããæé©ãªéžæã¯ã培åºçãªæ€çŽ¢ã«ãã£ãŠã®ã¿å¯èœã§ãïŒãã®ã¿ã¹ã¯ã¯ããæå°ã®è²ã§ã°ã©ããçè²ããããšåçã§ãããæ°åŠè
ã¯ããã«å¯Ÿããæå¹ãªè§£æ±ºçã¯ãªããšèããŠããŸãã ã貪欲ãªãã¢ãããŒãã¯ãææªã®å¯èœæ§ã§ã¯ãããŸããã
次ã®pã¬ãžã¹ã¿ã«ç©ççãªç©ºãããªãå Žåã¯ã©ãããã°ããã§ããïŒ ããšãã°ããã®ã³ãŒãã«ã¯ããããã5ã€ã®pã¬ãžã¹ã¿ã䜿çšããã³ãã³ãïŒ10ã12ã17ã19ïŒããããŸããå°ãªããšã1ã€ã«ååãªã¹ããŒã¹ããããŸããã ã¡ã¢ãªå
ã«ä¿æããå¿
èŠãããããšã¯æããã§ãã
ããããã¬ãžã¹ã¿ãŒã«ãããªãã¯äžéã ãããªãã¯ã¡ã¢ãªå
ã«ä¿æãããŸãããšèšãã ãã§ã¯ååã§ã¯ãããŸãããç§ãã¡ã®pã³ãŒãã®æ瀺ã¯ã¡ã¢ãªããã®å€ãšã»ãšãã©ã®å®éã®ããã»ããµã®ã³ãã³ããåŠçããæ¹æ³ãç¥ããŸããã 䜿çšæã«ã¡ã¢ãªããå€ãååŸããŠç»é²ããå¿
èŠããããŸãã ã©ãã ãïŒ å®éããã¹ãŠã®ã¬ãžã¹ã¿ãããžãŒã§ãããããã¡ã¢ãªå
ã®å€ãåé€ããããã§ãïŒ
ãã®æç¹ã§ãç§ãåŠãã çè«ã¯æšæž¬ãšãã¥ãŒãªã¹ãã£ãã¯ã§çµãããŸãã ç§ã¯ãååãªã¬ãžã¹ã¿ããªãããŒã ã®åã«ããäœåãªã¬ãžã¹ã¿ãã¡ã¢ãªã«æµã蟌ããããšã決ãããã®åŸãã¡ã¢ãªããããããèªã¿æ»ããŸããã ãããã£ãŠã泚ãåºãããã¬ãžã¹ã¿ã®ãåç¶æéãã§ã¯ããã®ã³ãã³ãã®å Žæã«ç©Žã衚瀺ããããã®ç©Žã§ä»ã®ç®çã«è§£æŸãããã¬ãžã¹ã¿ã䜿çšã§ããŸãã
ã³ãŒãããæŒãã®ããŠããèªã¿åã/æžã蟌ã¿ã³ãã³ããã¡ã¢ãªã«æ¿å
¥ã§ããããã«ããã«ã¯ãäžé圢åŒãå°ãå€æŽããå¿
èŠããããŸãïŒããã¯ãªã³ã¯ãªã¹ãã«ãªãããžã£ã³ãã¿ãŒã²ããã¯ãªãã»ãããšããŠã§ã¯ãªãããã€ã³ã¿ãŒãšããŠä¿åãããŸãïŒãªã¹ããæåã°ã©ãã«å€æïŒã pã³ãŒãã®çæäžã«ã
ããã¯ãããã®ããã«ãã€ã³ããã¯ã¹ã§ããŒã ãåç
§ããå¿
èŠããããŸãã ãã®ãããåå¥ã®å€äœãã¯ãã«ããããŸãã
typedef std::list< struct commandn>::iterator pcommandn;
struct commandn {
command cmd;
pcommandn tgt; //
commandn( const command& cmd) : cmd(cmd), tgt( NULL ) {}
};
std::list<commandn> pcode;
std::vector<pcommandn> pcodeidx;
inline int emit( const command& cmd) {
pcodeidx.push_back(pcode.insert(pcode.end(), commandn(cmd)));
return pcode.size()- 1 ;
}
inline int emit(command::opcodes opcode, regnum dest, regnum src1, regnum src2) {
return emit(command(opcode, dest, src1, src2));
}
inline int emit(command::opcodes opcode, regnum dest, short imm) {
return emit(command(opcode, dest, imm));
}
inline void fix( int index, command::opcodes opcode, regnum dest, short imm) {
*pcodeidx[index] = commandn(command(opcode, dest, imm));
}
ãžã£ã³ãã®ç®æšãžã®ãã€ã³ã¿ã¯ãpã³ãŒãã®çæäžã«èšå
¥ããã®ãå°é£ã§ãããžã£ã³ããäœæããæç¹ã§ã¯ãã¿ãŒã²ããã€ã³ããã¯ã¹ã¯æ¢ã«ããã£ãŠããŸãããã©ã®ããŒã ãååšãããã¯ãŸã äžæã§ãã ãŸã äœæãããŠããªãããŒã ã«ãã€ã³ã¿ã眮ãããšã«ã¯åé¡ããããŸãã ãããã£ãŠãpã³ãŒãã®è§£æãšçæã®çµäºåŸãçæãããã³ãŒãããã€ãã¹ããŠãžã£ã³ããã€ã³ã¿ãŒãé
眮ããŸãã 次ã«ãã¬ãžã¹ã¿ã®å²ãåœãŠã«å¿
èŠãªãã¹ãŠã®äœæ¥ãè¡ããŸãã æåŸã«ãpã³ãŒãããã¡ã€ã«ã«ãã³ãããåã«ãããããã·ãªã¢ã«åãããå¿
èŠããããŸã-åã³ãã³ãã®æçµã€ã³ããã¯ã¹ã決å®ãããžã£ã³ããªãã»ãããå
¥åããŸãã ãã®ããã«ã
int index;
ãã£ãŒã«ãã
struct commandn
ã«è¿œå ã
int index;
int main() {
yyparse();
//
for ( int i= 0 ; i<pcode.size(); i++)
if (pcodeidx[i]->cmd.opcode==command::jz)
pcodeidx[i]->tgt = pcodeidx[i+ 1 +pcodeidx[i]->cmd.imm];
// /
// ...
// /
int index = 0 ;
foreach(i, pcode) i->index = index++;
foreach(i, pcode)
if (i->cmd.opcode==command::jz)
i->cmd.imm = i->tgt->index-i->index- 1 ;
// -
// ...
}
ãã€ã³ãã¯å°ããïŒãåŠç/æé©åããå®è£
ããŸãã ãæ³åã®ãšãããã»ãšãã©ã®ã³ã³ãã€ã©ã³ãŒããå¿
èŠã§ãã
å®è£
ã¬ãžã¹ã¿ãå²ãåœãŠãããã«ãåã³ãã³ãã«å¯ŸããŠïŒ
struct commandn
çŽæ¥ïŒå
¥åããã³åºåã§æå¹ãªã¬ãžã¹ã¿ã®ã»ãããä¿åããŸããã©ã€ãã¿ã€ã ã決å®ããããã®pã³ãŒãã¬ãžã¹ã¿ã ããã³åå¥ã«-å®éã®ç®çãšã®äºææ§ãå€æããããã®ç©çã¬ãžã¹ã¿ã
typedef std::set<regnum> aliveset;
enum physreg { firstp = 1 , lastp = 4 };
typedef std::set<physreg> alivesetp;
struct commandn {
// ...
aliveset onenter, onexit; // liveness check
alivesetp onenterp, onexitp; // phys reg allocation
// ...
};
ãã1ã€ã®äŸ¿å©ãªããšã¯ãã³ãã³ãã§äœ¿çšãããã¬ãžã¹ã¿ããªãã³ãŒãã§å€æããããšã§ãã
inline bool hasnext(pcommandn cmd) {
return (cmd->cmd.opcode!=command::hlt &&!
(cmd->cmd.opcode==command::jz && !cmd->cmd.dest));
}
inline bool has2src(pcommandn cmd) {
return cmd->cmd.opcode>=command::add;
}
inline bool writesdest(pcommandn cmd) {
return cmd->cmd.opcode>=command::mov;
}
inline bool readsdest(pcommandn cmd) {
return cmd->cmd.opcode>=command::store &&
cmd->cmd.opcode<=command::echo;
}
ãã®åŸãäžã®å³ã«åŸã£ãŠãåã³ãã³ãã«ã€ããŠãå
¥åãšåºåã§å¿
èŠãªã¬ãžã¹ã¿ã®ã»ãããèšç®ããŸãã åæã«ã貪欲ãªã¢ãã€ã³ãã¡ã³ãã®ããã«ãåã¬ãžã¹ã¿ãå¿
èŠãªåæ°ãã«ãŠã³ãããŸãã
std::vector< int > liveness() { // returns usecount
std::vector< int > usecount(lastreg+ 1 );
bool changed = false ;
// rule 1
foreach(i, pcode) {
i->onenter = i->onexit = aliveset();
if (has2src(i)) {
if (i->cmd.src1) {
usecount[i->cmd.src1]++;
i->onenter.insert(i->cmd.src1);
}
if (i->cmd.src2) {
usecount[i->cmd.src2]++;
i->onenter.insert(i->cmd.src2);
}
} else if (readsdest(i) && i->cmd.dest) {
usecount[i->cmd.dest]++;
i->onenter.insert(i->cmd.dest);
}
if (i->onenter.size())
changed = true ;
}
while (changed) {
changed = false ;
foreach2(i, pcode, next) {
int oldsize = i->onenter.size()+i->onexit.size();
// rule 2 (next command)
if (hasnext(i))
i->onexit.insert(next->onenter.begin(), next->onenter.end());
// rule 2 (jmp target)
if (i->cmd.opcode==command::jz)
i->onexit.insert(i->tgt->onenter.begin(), i->tgt->onenter.end());
// rule 3
i->onenter.insert(i->onexit.begin(), i->onexit.end());
if (writesdest(i))
i->onenter.erase(i->cmd.dest);
if (i->onenter.size()+i->onexit.size() != oldsize)
changed = true ;
}
}
return usecount;
}
foreach2
ãã¯ãã
foreach2
ãããšãã«ãŒãã®æ¬æã®çŸåšã®èŠçŽ ãšæ¬¡ã®èŠçŽ ã確èªã§ããŸãã
#define foreach2(i, list, next) typedef typeof(list) TOKENPASTE2(T, __LINE__ ); \
for (TOKENPASTE2(T, __LINE__ )::iterator next = list.begin(), i=next++; i != list.end(); i=next, next++)
ååãšããŠãããŒã ããäžããäžãã§ã¯ãªããäžããäžãã«ãã€ãã¹ãããªãå Žåã¯ãåããŒã ããåã®ããŒã ã«ãåºãããå¿
èŠããããããå埩ã®ç·æ°ãæžããããšãã§ããŸãã ã³ãŒããç°¡çŽ åããããã«ãããããŠã³ãã€ãã¹ãéžæãããŸããã ææªã®å Žåãé·ã
Nã®ã³ãã³ãã®ã³ãŒãã§ã¯ã2
Nåã®å埩ãå¿
èŠã«ãªããŸãïŒå埩ããšã«ãã³ãã³ãã®ååã®è·é¢ãŸã§å¿
èŠã«ãªããŸãïŒã
test.jsk
ã«ã¯35åã®å埩ã§ååã§ããããžã£ã³ããããšè·é¢ãçããªãããã§ãã
æåŸã«ããªãããããã¹ãŠè¡ã£ãã®ãïŒ
// / :
while ( 1 ) { //
//
// ...
std::vector< int > usecount = liveness();
std::vector<physreg> physmap(lastreg+ 1 );
foreach(i, pcode)
i->onenterp = i->onexitp = alivesetp();
while ( 1 ) {
//
std::vector< int >::iterator max = std::max_element(usecount.begin(), usecount.end());
if (!*max) break ; //
*max = 0 ;
regnum r = max - usecount.begin();
// . - âr
std::vector< int > collisions(lastp); // 0-based
for (physreg p=firstp; p<=lastp; p=(physreg)(p+ 1 )) { // error: ISO C++ forbids incrementing an enum
foreach(i, pcode) {
if (contains(i->onenter, r) && contains(i->onenterp, p))
collisions[p- 1 ]++;
if (contains(i->onexit, r) && contains(i->onexitp, p))
collisions[p- 1 ]++;
}
if (collisions[p- 1 ]) continue ; // .
physmap[r] = p;
// - âr âp
foreach(i, pcode) {
if (contains(i->onenter, r))
i->onenterp.insert(p);
if (contains(i->onexit, r))
i->onexitp.insert(p);
}
break ; //
}
if (physmap[r]) continue ; //
// . - âr
// : ,
foreach2(i, pcode, next)
if ((i->cmd.opcode!=command::load && i->cmd.opcode!=command::store) &&
((contains(i->onenter, r) && i->onenterp.size()==lastp) ||
(contains(i->onexit, r) && i->onexitp.size()==lastp))) {
// , ,
// ...
goto afterspill;
}
// : ?
yyerror( "don't know how to proceed" );
}
// ;
foreach(i, pcode) {
i->cmd.dest = physmap[i->cmd.dest];
if (has2src(i)) {
i->cmd.src1 = physmap[i->cmd.src1];
i->cmd.src2 = physmap[i->cmd.src2];
}
}
break ;
afterspill : // - " "
}
ããã§ãã¬ãžã¹ã¿ãã©ã®ããã«æ³šããã«ã€ããŠæ£ç¢ºã«ã¯æãäžããŠããŸããããäžèšã®ã³ãŒãã«ã¯å€ãã®åé¡ãããããšã«èšåãã䟡å€ããããŸãã 第äžã«ããã¥ãŒãªã¹ãã£ãã¯ã¯æãåçŽãªå Žåã«ã®ã¿æ©èœããŸãã ããäžè¬çãªç¶æ³
...
10ã«ã¯r1ãr2ãr3ãr4ãå¿
èŠ
...
20ã«ã¯r1ãr3ãr4ãr5ãå¿
èŠ
...
30ã«ã¯r2ãr3ãr4ãr5ãå¿
èŠ
...
ãã¹ãŠã®ç©ç
r5
ã
r1,r2,r3,r4
è²»ããããããã
r5
ã«ã¯ç©ç
r5
ããããŸããã ãã ãã5ã€ã®ã¬ãžã¹ã¿ãã¹ãŠãåæã«å¿
èŠãªåäžã®ã³ãã³ãã¯ãããŸããã
ããé£ããåé¡ã¯ããã¹ãŠã®æµåºãç¡é§ãª
store/load
ãã¢ã§ããã°ã©ã ãè©°ãŸãããããšã§ããããããããããã«ããå¿
èŠããããŸã-ãããªããã°ããããã§äœ¿çšãããã¬ãžã¹ã¿ã¯ååãªããã€ãºããäœæããäœãã¬ãžã¹ã¿ããªããªããŸãã
å¥ã®ã¢ã€ãã¢ïŒæµåºã«èµ·å ããã©ã€ãã¿ã€ã ã®ç©Žã¯ãå
ã®pã¬ãžã¹ã¿ãç°ãªãç©çã¬ãžã¹ã¿ã«é
眮ã§ãã2ã€ã®ç¬ç«ãããããŒãžã§ã³ãã«åå²ããå¯èœæ§ããããŸãã ã·ã¹ãã ã®æè»æ§ã®ããã«ãããŒãžã§ã³åå²ãå®è£
ãã䟡å€ããããŸããå€éšãµã€ã¯ã«ãç¹°ãè¿ããã³ã«ãã¬ãžã¹ã¿ã®å¯¿åœãçããªããç©ççãªã¬ãžã¹ã¿ã®åæ£ãããç°¡åã«ãªããŸãã
ãããŠãäœåãª
load
ãš
store
ãåé€ããããšããã®ã§ãä»ã®äœåãªã³ãã³ããåæã«åé€ããŸãã pã³ãŒãã®æäœã«ã¯ä»ã®å¯äœçšããªãããšãèæ
®ãããšãçµæã§ã¬ãžã¹ã¿ãŒãèšç®ãã以å€ã«ãåºåã§çµæãçããŠããªãã³ãã³ããåé€ã§ããŸãã ãã¶ãããã°ã©ããŒã¯å€ãæªäœ¿çšã®å€æ°ã«å²ãåœãŠããã
(2+2);
ãããªæŒç®åã䜿çšããŸãã
(2+2);
è¡å
ã®ãã¹ãŠãåé€ããŸãããã®ãããããŒããžã®å°éäžèœã³ãŒãïŒããšãã°hltã®åŸã®jzïŒãåé€ãããžã£ã³ãã®ãã§ãŒã³ãå§çž®ããŸãïŒãžã£ã³ãã1åã®ãžã£ã³ãã®ãžã£ã³ãã«çœ®ãæããŸãïŒã
ã³ãã³ããåé€ããŠããã®äžã®ãã¹ãŠã®ãžã£ã³ããæ€çŽ¢ããã³ä¿®æ£ããªãããã«ããã«ã¯ãã³ãã³ãã
nop
ã«çœ®ãæãããšäŸ¿å©ã§ãã 次ã®ã³ãã³ãïŒ
jz r00, +0
ïŒã«ç¡æ¡ä»¶ãžã£ã³ããå²ãåœãŠãŸãã å©ç¹ã¯ã次ã®å埩ã§ã
nop
ã®ãžã£ã³ãããã§ãŒã³ã«æ²¿ã£ãŠå§çž®ããã
nop
èªäœãå°éäžèœãªããŒã ãšããŠã¯ãªãŒã³ã¢ãããããããšã§ãã
ã·ã³ãã«ãªæé©å
å°éäžèœãªããŒã ãæ€åºããããã«ã詳现ãªãã€ãã¹ã䜿çšããŸãã ã©ã®ã³ãã³ããæ¢ã«ãã€ãã¹ãããŠãããã远跡ããã«ã¯ã
struct commandn
int reachcnt;
æ°ãã
int reachcnt;
ãã£ãŒã«ããè¿œå ã
int reachcnt;
ããŒã«å€ãã£ãŒã«ãã§ååã§ãã ããããåããŒã ã®ããšã³ããªãŒã®çšåºŠãã¯ããã€ã圹ã«ç«ã€ããšãããããããããŸããã
void markReachable(pcommandn cmd) {
while (!cmd->reachcnt++) {
if (cmd->cmd.opcode==command::jz)
markReachable(cmd->tgt);
if (hasnext(cmd))
cmd++;
else
break ;
}
}
void nopOut(pcommandn cmd) {
pcommandn next = cmd; next++;
cmd->cmd = command(command::jz, 0 , 0 );
cmd->tgt = next;
}
void simpleopt() {
//
foreach(i, pcode)
if (i->cmd.opcode==command::jz)
while (i->tgt->cmd.opcode==command::jz && !i->tgt->cmd.dest)
i->tgt = i->tgt->tgt;
//
foreach(i, pcode) i->reachcnt = 0 ;
markReachable(pcode.begin());
foreach2(i, pcode, next)
if (!i->reachcnt)
pcode.erase(i);
// nop:
foreach2(i, pcode, next)
if (i->cmd.opcode==command::jz &&
!i->cmd.dest && i->tgt==next)
pcode.erase(i);
}
// :
simpleopt();
//
liveness();
bool changed = false ;
foreach(i, pcode)
if (writesdest(i) && !contains(i->onexit, i->cmd.dest)) {
nopOut(i);
changed = true ;
}
if (changed) continue ;
//
// ...
ããŒãžã§ã³åå²
ã¬ãžã¹ã¿ã®ãå¿
èŠééãã«ã®ã£ãããããå Žåãã€ãŸã ããåœä»€ã»ããã®å€ãå¥ã®ã»ããã«äŸµå
¥ããããšã¯ãããŸãããã€ãŸããã¬ãžã¹ã¿ããããçã寿åœã®ã¬ãžã¹ã¿ã®ãã¢ã«åå²ããããšãã§ããŸãã
å¥ã®ã°ã©ãã¢ã«ãŽãªãºã ã¯ãã©ã€ãã¿ã€ã ããäžé£ç¶ãªãã©ã°ã¡ã³ããæœåºããã®ã«åœ¹ç«ã¡ãŸãïŒæ¥ç¶ãããã³ã³ããŒãã³ãã®æ€çŽ¢ã
ç§ãã¡ã¯ãã³ãŒãã®æå¹æ§ãããã³ãŒãã®æå¿«ããè¿œæ±ããŠããŸãã ãããã£ãŠãpvgå
ã«pvgããããã°ã©ãå
šäœãèµ°æ»ããããšã§ã³ã³ããŒãã³ããããŒãžãããŸãã
struct commandn
ã¯ãä»æ¥ã®æåŸã®å¥ã®ãã£ãŒã«ããå¿
èŠã§ã
std::map<regnum,regnum> version;
ãã®äžã«ãå察å¿ã³ãã³ããp-register-> version numberããä¿åããŸãã
ã³ãã³ãã§å€§æåãšå°æåã䜿çšãããŠãããã
version
ãŸã äžèŽããŠããªãå Žåãåé¿çã¯ãŸã ãã®ã³ãã³ãã«å°éããŠããŸããã
å€éšpvgã®å Žåãæ°ãããã£ãŒã«ããéå§ããªãããã«ã
markReachable()
ãšåã
reachcnt
ã䜿çšããŸã
std::map<regnum,regnum> renameto; //
void versionizeReg(pcommandn cmd, regnum r, regnum rv) { // r rv
while (!contains(cmd- > version, r) &&
(contains(cmd- > onenter, r) || contains(cmd- > onexit, r))) {
//
cmd- > version[r] = renameto[rv];
if (cmd- > cmd.dest==r) cmd- > cmd.dest=renameto[rv];
if (has2src(cmd)) {
if (cmd- > cmd.src1==r) cmd- > cmd.src 1 =renameto[rv];
if (cmd- > cmd.src2==r) cmd- > cmd.src 2 =renameto[rv];
}
if (!contains(cmd- > onexit,r)) return ; //
if (cmd- > cmd.opcode==command::jz &&
contains(cmd- > tgt- > onenter, r)) {
versionizeReg(cmd- > tgt, r, rv);
}
if (hasnext(cmd)) {
cmd++;
if (contains(cmd- > onenter, r))
continue ;
}
return ; //
}
// ?
if (contains(cmd- > version, r) && cmd- > version[r]!=renameto[rv]) {
// renameto[rv] cmd->version[r]
regnum from = std::max(cmd- > version[r], renameto[rv]),
to = std::min(cmd- > version[r], renameto[rv]);
renameto[from] = to;
foreach(i, pcode) {
if (i- > cmd.dest==from) i- > cmd.dest = to;
if (has2src(i)) {
if (i- > cmd.src1==from) i- > cmd.src 1 = to;
if (i- > cmd.src2==from) i- > cmd.src 2 = to;
}
if (contains(i- > version, r) && i- > version[r]==from)
i- > version[r] = to;
}
}
}
void versionize(pcommandn cmd) {
while (!cmd- > reachcnt++) {
// versionize registers that live on exit
foreach(r, cmd- > onexit)
if (!contains(cmd- > version, *r)) {
regnum rv = newreg();
renameto[rv] = rv;
versionizeReg(cmd, *r, rv);
}
if (cmd- > cmd.opcode==command::jz)
versionize(cmd- > tgt);
if (hasnext(cmd))
cmd++;
else
break ;
}
}
// :
foreach(i, pcode) {
i- > version.clear();
i- > reachcnt = 0 ;
}
renameto.clear();
int lastbasereg = lastreg;
versionize(pcode.begin());
// , 1
foreach(i, pcode) {
if (i- > cmd.dest) i- > cmd.dest-=lastbasereg;
if (has2src(i)) {
if (i- > cmd.src1) i- > cmd.src 1 -=lastbasereg;
if (i- > cmd.src2) i- > cmd.src 2 -=lastbasereg;
}
}
lastreg -= lastbasereg;
å®éã®ã¬ãžã¹ã¿ãŒã®æ³šå
¥ãå®è£
ããããšã¯æ®ã£ãŠããŸãã ãã¥ãŒãªã¹ãã£ãã¯ãæå®ããŸãïŒçããŠããããã³ãã³ãã§äœ¿çšãããŠããªãã¬ãžã¹ã¿ã泚ãåºãããšãã§ããŸãã
regnum spillable(pcommandn cmd) { // ,
aliveset alive;
alive.insert(cmd->onenter.begin(), cmd->onenter.end());
alive.insert(cmd->onexit.begin(), cmd->onexit.end());
alive.erase(cmd->cmd.dest);
if (has2src(cmd)) {
alive.erase(cmd->cmd.src1);
alive.erase(cmd->cmd.src2);
}
if (!alive.size()) return 0 ;
return *alive.begin();
}
å¥ã®
map
ã¯ãã¬ãžã¹ã¿ã®ãã¹ãŠã®æµåºãåãå Žæã«åãŸãããã«ãã¬ãžã¹ã¿çªå·ã«ãã£ãŠãã®ã³ããŒã®ãã¢ãã¬ã¹ããã¡ã¢ãªã«ä¿åããŸãã ã¢ãã¬ã¹ã¯ãæåã®æµåºæã«ã®ã¿ã¬ãžã¹ã¿ã«å²ãåœãŠãããŸãã
// spill slots
std::map<regnum, int > spill;
int lastspill = 0 ;
void spillAt(pcommandn cmd, regnum victim, pcommandn next) {
// /
int spslot = spill[victim];
if (!spslot) {
spslot = ++lastspill;
spill[victim] = spslot;
}
// store load
pcommandn me = pcode.insert(next, *cmd);
cmd->cmd = command(command::store, victim, spslot);
commandn load(command(command::load, victim, spslot));
if (hasnext(me))
pcode.insert(next, load);
if (me->cmd.opcode==command::jz)
me->tgt = pcode.insert(me->tgt, load);
}
ããŒã ã¯
store-cmd-load
ããªãã«ã«çœ®ãæãããããããããŒã ãžã®æ¢åã®ãžã£ã³ãã¯ããã®åã«æ¿å
¥ããã
store
ã«ãžã£ã³ãããå¿
èŠããããŸãã ã³ãŒãå
šäœã§ãã®ãããªãžã£ã³ããæ¢ããªãããã«
cmd
次ã®ã³ãã³ãã§
cmd
ã³ããŒãæ¿å
¥ãã
å
ã®ã³ãã³ãã
store
眮ãæããŸãã
åæ§ã«ãã³ãã³ããæ¡ä»¶ä»ããžã£ã³ãã®å Žåãäž¡æ¹ã®çµæã§
load
å®è¡ããå¿
èŠããã
load
ã ãããã£ãŠã次ã®ã³ãã³ãã§è¿œå ãããžã£ã³ãã®ç®æšã®åã«æ¿å
¥ããŸãã
ïŒã³ãã³ããããã³ãã³ãããå¥ã®ã³ãã³ãã«ç§»åããå¯èœæ§ããããããæåã®ããªãŒå®è£
ã®ããã«ã
echo
ã®è¡ããèå¥åãã«ãã£ãŠã³ãã³ãèªäœã«ãã€ã³ãããå¿
èŠããããŸããã³ãã³ãã€ã³ããã¯ã¹ããã€ã³ã¿ãå€æŽãããŸãããïŒ
2çªç®ã®ãã¥ãŒãªã¹ãã£ãã¯ïŒãã¹ãŠã®ç©çã¬ãžã¹ã¿ãŒãå æãããŠããããŒã ãèŠã€ããããšãã§ããªãã£ãå Žåããæé©ãªãç©çã¬ãžã¹ã¿ãŒïŒæ¢ã«å²ãåœãŠãããŠããã¬ãžã¹ã¿ãŒãšã®è¡çªãæãå°ãªãïŒãååŸããå¿
èŠãªå Žæã§è§£æŸããŸãã
// . - âr
regnum victim;
// 1: ,
foreach2(i, pcode, next)
if ((i->cmd.opcode!=command::load && i->cmd.opcode!=command::store) &&
((contains(i->onenter, r) && i->onenterp.size()==lastp) ||
(contains(i->onexit, r) && i->onexitp.size()==lastp))) {
victim = spillable(i);
assert(victim);
spillAt(i, victim, next);
goto afterspill;
}
// 2: " "
physreg bestfit = (physreg)(std::max_element(collisions.begin(), collisions.end())-collisions.begin()+ 1 );
foreach2(i, pcode, next)
if ((i->cmd.opcode!=command::load && i->cmd.opcode!=command::store) &&
((contains(i->onenter, r) && contains(i->onenterp, bestfit)) ||
(contains(i->onexit, r) && contains(i->onexitp, bestfit))) &&
(victim = spillable(i))) {
spillAt(i, victim, next);
goto afterspill;
}
yyerror( "don't know how to proceed" );
ããŒãžã§ã³ãåå²ãããšãã¯ãã¹ãããçªå·ãã³ããŒããããšãå¿ããªãã§ãã ããïŒåãã¬ãžã¹ã¿ã®ãã¹ãŠã®ããŒãžã§ã³ã1ã€ã®ã¹ãããã«ä¿åãããŸãïŒã次ã«ãããŒãžã§ã³ã®ååãå€æŽãããšãã¯ã
spill
ãæŽæ°ããããšãå¿ããªãã§ãã ããã
ã³ã³ãã€ã©ã¯ã»ãšãã©æ©èœããŠããŸãã æåŸã®åé¡ã¯ãç¡æå³ãª
load
ãš
store
倧éã«çæããããããã®ããã«æå³ã®ããã³ãã³ãçšã®ã¬ãžã¹ã¿ããªãããšã§ãã
ã¡ã¢ãªãæäœãã
æåã«ãäžéšã®ã¬ãžã¹ã¿ã
load/store
ã³ãã³ãã§æä»çã«äœ¿çšãããããšãå€æããå ŽåããããŸãã
ãã®ãããªã³ãã³ãã¯ç¡æå³ã§ããã¬ãžã¹ã¿ã¯åžžã«ãèªã¿åãããã¹ããããšåãã¹ãããã«æžã蟌ãŸããŸãã
ããŒãžã§ã³ãåå²ããåŸã«ã¯ãªãŒã³ã¢ãããæ¿å
¥ããŸããçãã©ã€ãã¿ã€ã ã§å€ãã®ã¬ãžã¹ã¿ãååŸããå¿
èŠããããŸãã
//
std::vector< bool > used(lastreg+ 1 );
foreach(i, pcode) {
if (writesdest(i) && i->cmd.opcode!=command::load)
used[i->cmd.dest] = true ;
if (readsdest(i) && i->cmd.opcode!=command::store)
used[i->cmd.dest] = true ;
if (has2src(i)) {
used[i->cmd.src1] = true ;
used[i->cmd.src2] = true ;
}
}
foreach(i, pcode)
if ((i->cmd.opcode==command::load && !used[i->cmd.dest]) ||
(i->cmd.opcode==command::store && !used[i->cmd.dest]))
nopOut(i);
第äºã«ãåã
load
ãã§ãŒã³ãããå Žåãèªã¿åãã¬ãžã¹ã¿ãçããŠããªããããæåŸã®ãã§ãŒã³ä»¥å€ã¯ãã¹ãŠåé€ãããŸãã ããããåã
store
ãã§ãŒã³ãããå Žåãããããåé€ããã«ã¯ãäœãæ°ãããã®ãèãåºãå¿
èŠããããŸãã ãã®ã¢ã«ãŽãªãºã ã¯ãã¬ãžã¹ã¿ã®å¿
èŠæ§ã®å埩å®çŸ©ã«éåžžã«äŒŒãŠããŸããããä¿åããªãããå®çŸ©ããŠããã§ã«ä¿åãããŠããã¬ãžã¹ã¿ã®ãã¹ãŠã®ä¿åãã¯ãªã¢ããŸãã
- ã¬ãžã¹ã¿ã¯ãå€ãèšå®ããã³ãã³ãã«ã¯ä¿åãããŸããã
- ããŒã AããïŒçŽæ¥ãŸãã¯ãžã£ã³ãã§ïŒBã«ç§»åã§ããã¬ãžã¹ã¿ãAã«ä¿åãããŠããªãå ŽåãBã«ã¯ä¿åãããŸããã
- ã¬ãžã¹ã¿ã¯ããã®å€ãä¿åããã³ãã³ãã«ä¿åãããŸãã
ãã®ã¢ã«ãŽãªãºã ã¯ãé
åžã®æ¹åã®ã¿ãåã®ã¢ã«ãŽãªãºã ãšç°ãªãããšãããããŸããå¿
èŠæ§ã¯åããŒã ããåã®ããŒã ã«ãŸã§æ¡åŒµãããéä¿åã¯æ¬¡ã®ããŒã ã«æ¡åŒµãããŸãã
äžè¬ã«ãããã°ã©ã ã®åãã€ã³ãã§ã¬ãžã¹ã¿ã®ã»ãããèšç®ããããã®å埩ã¢ã«ãŽãªãºã ã¯ã
ããŒã¿ãããŒåæãšåŒã°ãã2ã€ã®ååãé€ããŠãä»ã®å€ãã®æé©åã«äœ¿çšãããŸãã
void unsaved() {
bool changed = false ;
// rule 1
foreach(i, pcode) {
i->onenter = i->onexit = aliveset();
if (writesdest(i)) {
i->onexit.insert(i->cmd.dest);
changed = true ;
}
}
while (changed) {
changed = false ;
foreach2(i, pcode, next) {
int oldsize = i->onenter.size()+i->onexit.size();
// rule 2 (next command)
if (hasnext(i))
next->onenter.insert(i->onexit.begin(), i->onexit.end());
// rule 2 (jmp target)
if (i->cmd.opcode==command::jz)
i->tgt->onenter.insert(i->onexit.begin(), i->onexit.end());
// rule 3
i->onexit.insert(i->onenter.begin(), i->onenter.end());
if (i->cmd.opcode==command::store)
i->onexit.erase(i->cmd.dest);
if (i->onenter.size()+i->onexit.size() != oldsize)
changed = true ;
}
}
}
afterspill : // - " "
unsaved();
foreach(i, pcode)
if (i->cmd.opcode==command::store && !contains(i->onenter, i->cmd.dest))
nopOut(i);
}
ã©ãããã®ïŒ
ã³ã³ãã€ã©ãŒå
šäœïŒ
tyomitch.net.ru/jsk.y.regs.html5äžè¡ã®ãã¡ã解æã®5åã®1ã ããå±ããŸãã å®éãä»ã®ã³ã³ãã€ã©ã®äººä»¶è²»ãšæ¯èŒããŠãããŒãµãŒã¯é¢çœããªãäºçŽ°ãªããšã®ããã§ãã ããããããã¯ãããŒãµãŒã®äœæã骚ã«åžã蟌ãŸããæºåãã§ããŠãããã®ãã䜿çšã§ããªãããã§ãã äžæ¹ãã³ãŒããåŠçãããšãããã£ã¯ã·ã§ã³ã®äœå°ããããŸãã
ã¬ãžã¹ã¿ã®å²ãåœãŠãšæåã®æé©åã®çµæãçæãããã³ãŒãã¯ããã€ãã®ã³ãã³ãã®ã¿ã§æ¡åŒµãããŸããã
00 mov r01,0
01 mov r02ã0x3e8
02ãšã³ãŒ0x12e
03 echo r01
04ãšã³ãŒ0xa8
05ãšã³ãŒr02
06ãšã³ãŒ0xaf
07 le r03ãr01ãr02
08 jz r03ã+ 0x1fïŒ= 0x28ïŒ
09 r03ãr01ãr02ãè¿œå
0a mov r04ã2
0b div r03ãr03ãr04
0cãšã³ãŒ0x16a
0dãšã³ãŒr03
0eãšã³ãŒ0xd4
0få
¥år04
10åºèr01ã1
11 mov r01ã1
12 eq r01ãr04ãr01
13 jz r01ã+ 5ïŒ= 0x19ïŒ
14ããŒãr01ã1
15 mov r02ã1
16ãµãr02ãr03ãr02
17 r02ãr02ã0ãè¿œå
18 jz 0ã-0x12ïŒ= 0x7ïŒ
19 mov r01ã2
1a eq r01ãr04ãr01
1b jz r01ã+ 4ïŒ= 0x20ïŒ
1c mov r01ã1
1d r01ãr03ãr01ãè¿œå
1e r01ãr01ã0ãè¿œå
1f jz 0ã-0x19ïŒ= 0x7ïŒ
20ããŒãr01ã1
21 mov r03ã3
22 eq r03ãr04ãr03
23 jz r03ã+ 2ïŒ= 0x26ïŒ
24ãšã³ãŒ0x14e
25 hlt
26ãšã³ãŒ0x172
27 jz 0ã-0x21ïŒ= 7ïŒ
28ãšã³ãŒ0x107
29 hlt
ãã ãã䜿çšãããã¬ãžã¹ã¿ã¯4ã€ã ããªã®ã§ããã®pã³ãŒãã¯ããããå®éã®ããã»ããµã®ãã·ã³ã³ãŒãã«å€æãããŸãã
8æãç¶ç¶ããŸããäŒæã«è¡ãã®ã§ããã®æçš¿ã空枯ããçŽæ¥è¿œå ããŸãã
ã¿ãªããçŽ æµãªå€ãïŒ