ããŒã1ïŒã¯ããã«ãšåå¥è§£æ
ããŒã2ïŒããŒãµãŒãšASTã®å®è£
ããŒã3ïŒLLVM IRã³ãŒãçæ
ããŒã4ïŒJITããã³ãªããã£ãã€ã¶ãŒãµããŒãã®è¿œå
ããŒã5ïŒèšèªæ¡åŒµïŒå¶åŸ¡ãããŒ
ããŒã6ïŒèšèªæ¡åŒµïŒãŠãŒã¶ãŒå®çŸ©æŒç®å
ããŒã7ïŒèšèªæ¡åŒµïŒå¯å€å€æ°
ããŒã8ïŒãªããžã§ã¯ãã³ãŒããžã®ã³ã³ãã€ã«
ããŒã9ïŒãããã°æ å ±ã®è¿œå
ããŒã10ïŒçµè«ãšãã®ä»ã®LLVMã®å©ç¹
9.1ã ãããã«
ãã¥ãŒããªã¢ã«ãLLVMã䜿çšããããã°ã©ãã³ã°èšèªã®äœæãã®æåŸã®éšåã«ããããã ãã®ãã¥ãŒããªã¢ã«ãéããŠãå°ããªäžè¯é¡ã®èšèªã圹ã«ç«ããªãããã¡ãããããªãé¢çœãïŒãããã圹ã«ç«ããªããããããŸãããïŒããã¡ãã«æé·ãããŸããã
ç§ãã¡ãã©ããŸã§æ¥ãã®ãããããå¿ èŠãšããã³ãŒããã©ãã ãå°ãªãã®ããèŠãã®ã¯èå³æ·±ãã§ãã å®å šãªã¬ãã·ã«ã«ã¢ãã©ã€ã¶ãŒãããŒãµãŒãASTãã³ãŒããžã§ãã¬ãŒã¿ãŒã察話åå®è¡ïŒJITã䜿çšïŒïŒãããã³ãããã°æ å ±ãã¹ã¿ã³ãã¢ãã³ã®å®è¡å¯èœãã¡ã€ã«ã«çæããŸãã-ããã¯ãã¹ãŠã1000è¡æªæºã®wadïŒç©ºã®è¡ãšã³ã¡ã³ããé€ãïŒã§ãã
ç§ãã¡ã®å°ããªèšèªã¯ãããã€ãã®èå³æ·±ãæ©èœããµããŒãããŠããŸãããŠãŒã¶ãŒå®çŸ©ã®äºé æŒç®åãšåé æŒç®åããµããŒãããJITã³ã³ãã€ã«ã䜿çšããŠå³åº§ã«å®è¡ããSSA圢åŒã§ã³ãŒããçæããŠãããŒå¶åŸ¡æ§é ããµããŒãããŸãã
ãã®ã¬ã€ãã®ã¢ã€ãã¢ã®äžéšã¯ãèšèªã®å®çŸ©ãæ§ç¯ãããã³æäœãã©ãã»ã©ç°¡åãã瀺ãããšã§ããã ã³ã³ãã€ã©ã®æ§ç¯ã¯ãæãããããã»ã¹ãç¥ç§çãªããã»ã¹ã§ããå¿ èŠã¯ãããŸããïŒ åºæ¬ãç解ããã®ã§ãã³ãŒããååŸããŠå¯ŸåŠããããšã匷ããå§ãããŸãã ããšãã°ãè¿œå ããŠã¿ãŠãã ããïŒ
ã°ããŒãã«å€æ°-çŸä»£ã®ãœãããŠã§ã¢å·¥åŠã«ãããã°ããŒãã«å€æ°ã®å€ã¯çããããã®ã§ããããããã¯å€ãã®å ŽåãKaleidoscopeã³ã³ãã€ã©èªäœãªã©ã®å°ããªé«éããã¯ã«äœ¿çšãããŸãã 幞ããªããšã«ãããã°ã©ã ã«ã°ããŒãã«å€æ°ãè¿œå ããã®ã¯éåžžã«ç°¡åã§ããåå€æ°ãã°ããŒãã«ã·ã³ãã«ããŒãã«ã«ãããã©ããã確èªããã ãã§ãã ãã®ããã«ã æ°ããã°ããŒãã«å€æ°ãäœæããã«ã¯ãLLVM GlobalVariableã¯ã©ã¹ã®ã€ã³ã¹ã¿ã³ã¹ãäœæããŸãã
åä»ãå€æ°-Kaleidoscopeã¯doubleå€æ°ã®ã¿ããµããŒãããããã«ãªããŸããã ããã«ãããèšèªãéåžžã«ãšã¬ã¬ã³ãã«ãªããŸãã1ã€ã®åã®ã¿ããµããŒãããããšã¯ãå€æ°ã®åãæå®ããå¿ èŠããªãããã§ãã èšèªãç°ãªãã°ããã®åé¡ã解決ããæ¹æ³ãç°ãªããŸãã æãç°¡åãªæ¹æ³ã¯ããŠãŒã¶ãŒã«åå€æ°å®çŸ©ã®ã¿ã€ããæå®ããããèŠæ±ããå€*ãšãšãã«ã·ã³ãã«ããŒãã«ã«å€æ°ã®ã¿ã€ããæžã蟌ãããšã§ãã
é åãæ§é ããã¯ãã«ãªã© ã¿ã€ããå ¥åãããšãããŸããŸãªèå³æ·±ãæ¹åã«ã¿ã€ãã·ã¹ãã ãæ¡åŒµãå§ããããšãã§ããŸãã åçŽãªé åã¯ãããŸããŸãªã¿ã€ãã®ã¢ããªã±ãŒã·ã§ã³ã«éåžžã«åçŽã§äŸ¿å©ãªãã®ã«ããããšãã§ããŸãã LLVM getelementptråœä»€ãã©ã®ããã«æ©èœããããåŠç¿ããããã®æŒç¿ãšããŠè¿œå ããŠãã ãããéåžžã«ãšã¬ã¬ã³ãã§çãããããç¬èªã®FAQããããŸãã
æšæºã©ã³ã¿ã€ã -çŸåšã®åœ¢åŒã§ã¯ãèšèªã¯ãŠãŒã¶ãŒã«ä»»æã®å€éšé¢æ°ã«ã¢ã¯ã»ã¹ããæ©èœãæäŸããŸããããããprintdãããputchardããªã©ã«äœ¿çšããŸãã èšèªãæ¡åŒµããŠãããé«ãã¬ãã«ã®æ§æäœãè¿œå ã§ããŸããå€ãã®å Žåããã®ãããªæ§æäœãã³ãã³ãã®ã€ã³ã©ã€ã³ã·ãŒã±ã³ã¹ã®åœ¢åŒã§äœæããããããã©ã³ã¿ã€ã é¢æ°ã«æã£ãŠè¡ãæ¹ãçã«ããªã£ãŠããŸãã
ã¡ã¢ãªç®¡ç-ã«ã¬ã€ãã¹ã³ãŒãèšèªã§ã¯ãã¹ã¿ãã¯ã®ã¿ã«ã¢ã¯ã»ã¹ã§ããŸãã ãŸããæšæºã®libc malloc / freeã€ã³ã¿ãŒãã§ã€ã¹ãåŒã³åºãããã¬ããŒãžã³ã¬ã¯ã¿ãŒã䜿çšããŠãããŒãã«ã¡ã¢ãªãå²ãåœãŠããšäŸ¿å©ã§ãã ã¬ããŒãžã³ã¬ã¯ã¿ã䜿çšããå Žåã¯ãLLVMããªããžã§ã¯ãã®ç§»åã¢ã«ãŽãªãºã ãã¹ã¿ãã¯ã®ã¹ãã£ã³/æŽæ°ã«å¿ èŠãªã¢ã«ãŽãªãºã ãå«ãAccurate Garbage Collectionãå®å šã«ãµããŒãããŠããããšã«æ³šæããŠãã ããã
äŸå€ãµããŒã-LLVMã¯ããŒãã³ã¹ãã§ãä»ã®èšèªã§ã³ã³ãã€ã«ãããã³ãŒããšå¯Ÿè©±ããæ©èœãåããäŸå€ã®çæããµããŒãããŸãã åé¢æ°ããšã©ãŒå€ãè¿ããããããã§ãã¯ããããšãæ瀺ããã³ãŒããçæããããšãã§ããŸãã setjmp / longjmpãæ瀺çã«äœ¿çšããŠãäŸå€ãå®è£ ããããšãã§ããŸãã äžè¬ã«ãå€ãã®ç°ãªãæ¹æ³ããããŸãã
OOPããžã§ããªãã¯åãããŒã¿ããŒã¹ãžã®ã¢ã¯ã»ã¹ãè€çŽ æ°ã幟äœåŠçããã°ã©ãã³ã°ã...å®éãèšèªã«è¿œå ã§ããã¯ã¬ã€ãžãŒãªãã®ã«çµããã¯ãããŸããã
çããã¢ããªã±ãŒã·ã§ã³-LLVMãå€ãã®äººãèå³ãæã£ãŠããåéã§äœ¿çšããããšã«ã€ããŠè©±ããŸããïŒç¹å®ã®èšèªçšã®ã³ã³ãã€ã©ã®æ§ç¯ã ãã ããäžèŠã³ã³ãã€ã©ã®äœ¿çšãèæ ®ãããŠããªãä»ã®å€ãã®é åããããŸãã ããšãã°ãLLVMã¯OpenGLã°ã©ãã£ãã¯ã¹ã®é«éåãC ++ã³ãŒãã®ActionScriptãžã®å€æãããã³ãã®ä»å€ãã®èå³æ·±ãããšã®ããã«äœ¿çšãããŸãã ãããããLLVMã䜿çšããŠãã€ãã£ãã®æ£èŠè¡šçŸã³ãŒãã«JITã³ã³ãã€ã©ãæ§ç¯ããæåã®äººã«ãªãã§ããããïŒ
åã³-ã¯ã¬ã€ãžãŒã§çããããšãããŠã¿ãŠãã ããã èšèªãä»ã®äººãšåãã«ããããšã¯ãäœããçãããã»ã©æ¥œãããããŸããã ããã«ã€ããŠè©±ãããå Žåã¯ãllvm-devã¡ãŒãªã³ã°ãªã¹ãã«æ°è»œã«æžã蟌ãã§ãã ãããèšèªã«èå³ãããããã°ãã°å©ããã人ãããããããŸãã
ãã¥ãŒããªã¢ã«ãå®äºããåã«ãLLVM IRãçæããããã®ãã³ããæäŸããããšæããŸãã æããã§ã¯ãªããããããªã埮åŠãªç¹ãããã€ããããŸãããLLVMã®æ©èœãå©çšãããå Žåã«ã¯éåžžã«äŸ¿å©ã§ãã
10.2ã LLVM IRããããã£
LLVM IRã³ãŒãã«ã€ããŠã¯2ã3ã®è³ªåããããŸããããããä»èŠãŠã¿ãŸãããã
10.2.1ã ã¿ãŒã²ãããã©ãããã©ãŒã ã®ç¬ç«
äžè¯é¡ã¯ãããŒã¿ãã«èšèªãã®äžäŸã§ããäžè¯é¡ã§æžãããããã°ã©ã ã¯ããããèµ·åãããã¿ãŒã²ãããã©ãããã©ãŒã äžã§åãããã«åäœããŸãã LispãJavaãHaskellãJavascriptãPythonãªã©ãä»ã®å€ãã®èšèªã«ãåãããããã£ããããŸãã ïŒãããã®èšèªã¯ç§»æ€å¯èœã§ããããã¹ãŠã®ã©ã€ãã©ãªã移æ€å¯èœã§ããããã§ã¯ãªãããšã«æ³šæããŠãã ããïŒã
LLVMã®åªããåŽé¢ã®1ã€ã¯ãIRã¬ãã«ã§ã¿ãŒã²ãããã©ãããã©ãŒã ããã®ç¬ç«æ§ãç¶æããããšã§ãïŒKaleidoscopeã§ã³ã³ãã€ã«ãããããã°ã©ã ã®LLVM IRãååŸããLLVMã§ãµããŒããããä»»æã®ã¿ãŒã²ãããã©ãããã©ãŒã ã§å®è¡ã§ããŸãããã€ãã£ãã«ãµããŒãããŸãã Kaleidoscopeã³ã³ãã€ã©ãŒã¯ãã³ãŒããçæãããšãã«ãã©ãããã©ãŒã ã«é¢ããæ å ±ãèŠæ±ããªãããããã©ãããã©ãŒã ã«äŸåããªãã³ãŒããçæãããšèšããŸãã
LLVMããã©ãããã©ãŒã ã«äŸåããªãã³ã³ãã¯ããªã³ãŒã衚瀺ãæäŸãããšããäºå®ã¯éåžžã«é åçã§ãã æ®å¿µãªããã人ã ã¯ãã°ãã°èšèªã®ç§»æ€æ§ã«ã€ããŠå°ãããšããCãŸãã¯Cã«äŒŒãèšèªã®ã³ã³ãã€ã«ã«ã€ããŠèããã ãã§ãã ãæ®å¿µãªããããšèšã£ãã®ã¯ãå®éã«ã¯Cã³ãŒãã移æ€ããããšã¯ïŒäžè¬çãªå ŽåïŒäžå¯èœã§ããããã§ãããã¡ããã移æ€ã®å Žåã§ããCãœãŒã¹ã³ãŒãèªäœã¯äžè¬çãªå Žå移æ€æ§ããªãããã§ãã 32ã64ãããã®ã¢ããªã±ãŒã·ã§ã³ã
Cã®åé¡ïŒãããäžè¬çãªã±ãŒã¹ã§ãïŒã¯ããã©ãããã©ãŒã åºæã®ä»®å®ã«å€§ããäŸåããŠããããšã§ãã ç°¡åãªäŸãšããŠãããªããã»ããµã¯æ¬¡ã®ããã¹ããåŠçããå Žåãã³ãŒããã©ãããã©ãŒã ãäŸåãããŸãã
#ifdef __i386__ int X = 1; #else int X = 42; #endif
ãã®åé¡ãããŸããŸãªè€éãªæ¹æ³ã§è§£æ±ºããããšã¯å¯èœã§ãããäžè¬çãªæ¹æ³ã§ã¯è§£æ±ºã§ããŸããã
ãã ããCã®ãµãã»ããã¯ç§»æ€å¯èœã«ããããšãã§ããŸãã åºå®ãµã€ãºïŒããšãã°ãint = 32ããããlong = 64ãããïŒã®ããªããã£ãåãäœæããå Žåãæ¢åã®ãã€ããªãã¡ã€ã«ãšã®ABIäºææ§ã«ã€ããŠå¿é ããã«ãä»ã®æ©èœãæŸæ£ããã°ã移æ€å¯èœãªã³ãŒããååŸã§ããŸãã ããã¯ããã€ãã®ç¹å¥ãªå Žåã«æå³ããããŸãã
10.2.2ã ã»ãã¥ãªãã£ä¿èšŒ
èšåãããèšèªã®å€ãã¯ãå®å šãã§ããããŸããJavaã§æžãããããã°ã©ã ãã¢ãã¬ã¹ç©ºéãå°ç¡ãã«ããŠããã»ã¹ãèœãšãããšã¯äžå¯èœã§ãïŒJVMã«ãã°ããªããšä»®å®ïŒã ã»ãã¥ãªãã£ã¯ãèšèªèšèšãã©ã³ã¿ã€ã ãµããŒããããã³å€ãã®å ŽåOSãµããŒãã®çµã¿åãããå¿ èŠãšããèå³æ·±ãæ©èœã§ãã
LLVMã§å®å šãªèšèªãå®è£ ããããšã¯ç¢ºãã«å¯èœã§ãããLLVM IRã ãã§ã¯ã»ãã¥ãªãã£ãä¿èšŒããŸããã LLVM IRã§ã¯ãå®å šã§ãªããã€ã³ã¿ãŒå€æã解æŸåŸã®ã¡ã¢ãªãŒäœ¿çšããããã¡ãŒãªãŒããŒãããŒããã®ä»ã®ããŸããŸãªåé¡ãçºçããŸãã ã»ãã¥ãªãã£ã¯LLVMãããé«ãã¬ãã«ã§å®è£ ããå¿ èŠãããã幞ããªããšã«ãããã€ãã®ã°ã«ãŒãããã®åé¡ã調æ»ããŠããŸãã 詳现ã«ã€ããŠã¯ãllvm-devã¡ãŒãªã³ã°ãªã¹ãã§è³ªåããŠãã ããã
10.2.3ã èšèªåºæã®æé©å
LLVMã«ã¯ãå€ãã®äººãæ°ã«å ¥ããªããã®ã1ã€ãããŸãã1ã€ã®ã·ã¹ãã ã§äžçã®ãã¹ãŠã®åé¡ã解決ããããã§ã¯ãããŸããïŒç³ãèš³ãããŸãããã飢ããŠããåäŸãä»ã®èª°ããããªãã®åé¡ã解決ããå¿ èŠããããŸãã LLVMããã®äžæºã®1ã€ã¯ãèšèªåºæã®é«åºŠãªæé©åãå®è¡ã§ããªãããšã§ããLLVMã¯ãæ å ±ã倱ããããŸããã
æ®å¿µãªããããã³ã³ãã€ã©ãŒèšèšã®çè«ãã®å®å šã§æ®éçãªããŒãžã§ã³ãæžãå Žæã¯ãããŸããã 代ããã«ãããã€ãã®èŠ³å¯ãè¡ããŸãã
LLVMã¯æ å ±ã倱ã£ãŠããŸãã ããšãã°ãSSAå€ãILP32ãã·ã³ã®ã¿ã€ãCãintããŸãã¯ãlongãããçæããããã©ãããLLVM IRã¬ãã«ã§åºå¥ããããšã¯ã§ããŸããïŒãããã°æ å ±ãé€ãïŒã ã©ã¡ããã¿ã€ããi32ãã®å€ã«ã³ã³ãã€ã«ããããœãŒã¹ã¿ã€ãã«é¢ããæ å ±ã¯å€±ãããŸãã ããäžè¬çãªåé¡ã¯ãLLVMåã·ã¹ãã ããåãååã®åçã®åã§ã¯ãªããåãæ§é ãæã€åãèæ ®ããããšã§ãã ããã¯ãåãæ§é ãæã€é«ã¬ãã«èšèªã®2ã€ã®åïŒããšãã°ã1ã€ã®intãã£ãŒã«ããæã€2ã€ã®ç°ãªãæ§é ïŒãããå Žåããããã®åã1ã€ã®LLVMåã«ã³ã³ãã€ã«ãããäžå¯èœã«ãªãããšã人ã ãé©ãããŸãå€æ°ãã©ã®åææ§é ã«å±ããŠããããèšããŸãã
第äºã«ãLLVMã¯æ å ±ã倱ã£ãŠããŸãããã¿ãŒã²ãããã©ãããã©ãŒã ãåºå®ãããŠããŸãããããŸããŸãªæ¹åã«æ å ±ãæ¡åŒµããã³æ¹åãç¶ããŠããŸãã æ°ããæ©èœãè¿œå ããŸãïŒLLVMã¯åžžã«äŸå€ããããã°æ å ±ããµããŒãããŠããŸããã§ããïŒãIRãæ¡åŒµããŠæé©åã«éèŠãªæ å ±ïŒåŒæ°ããŒããŸãã¯ç¬Šå·ä»ããããã§æ¡åŒµããããã©ããããã€ã³ã¿ãŒã®ãšã€ãªã¢ã¹ã«é¢ããæ å ±ãªã©ïŒããã£ããã£ããŸãã å€ãã®æ¹åã¯ãŠãŒã¶ãŒã«ãã£ãŠéå§ãããŸãã人ã ã¯LLVMã«ç¹å®ã®æ©èœãæãããããšæã£ãŠããŸãã
第äžã«ãèšèªåºæã®æé©åãç°¡åã«è¿œå ããããšãå¯èœã§ããããããè¡ãæ¹æ³ã¯ããã€ããããŸãã ç°¡åãªäŸãšããŠããœãŒã¹ã³ãŒãã«ã€ããŠããŸããŸãªããšããç¥ã£ãŠãããæé©åãã¹ãç°¡åã«è¿œå ã§ããŸãã Cã«äŒŒãèšèªã®å Žåããã®æé©åãã¹ã¯æšæºCã©ã€ãã©ãªã®é¢æ°ããèªèãããŸããmainïŒïŒã§é¢æ°ãexitïŒ0ïŒããåŒã³åºããšãåŒã³åºãã¯å®å šã«ãreturn 0ãã«å€æã§ããããšãããããŸããæšæºCã¯ãåºå£é¢æ°ãè¡ãã¹ãããšãèšè¿°ããŠããŸãã
ã©ã€ãã©ãªã®åçŽãªç¥èã«å ããŠãLLVM IRã«ä»ã®ããŸããŸãªèšèªåºæã®æ å ±ãåã蟌ãããšãã§ããŸãã ç¹å®ã®ããŒãºãããå Žåã¯ãllvm-devã¡ãŒãªã³ã°ãªã¹ãã«æžã蟌ãã§ãã ããã ææªã®ã·ããªãªã§ã¯ãLLVMãããã ã³ãŒããžã§ãã¬ãŒã¿ãŒããšã¿ãªããèšèªåºæã®ASTã§ããã³ããšã³ãã«é«ã¬ãã«ã®æé©åãå®è£ ã§ããŸãã
10.3ã ããªãã¯ãšããªãã¯
LLVMã§äœæ¥ããåŸãããŸããŸãªäŸ¿å©ãªããªãã¯ãããªãã¯ããããŸããããããã¯äžèŠããŠæããã§ã¯ãããŸããã 誰ããããããåçºèŠããªãããã«ããã®ã»ã¯ã·ã§ã³ã§ã¯ãããã®ããã€ãã«å°å¿µããŸãã
10.3.1ã ããŒã¿ãã«offsetof / sizeofã®å®è£
èå³æ·±ãã®ã¯ãã³ã³ãã€ã©ã«ãã£ãŠçæãããã³ãŒããããã©ãããã©ãŒã ã«äŸåããªããç¶æ ã«ä¿ã¡ããå ŽåãLLVMã¿ã€ãã®ãµã€ãºãšæ§é å ã®ç¹å®ã®ãµããã¥ãŒã®ãªãã»ãããç¥ãå¿ èŠããããšããããšã§ãã ããšãã°ãã¡ã¢ãªãå²ãåœãŠãé¢æ°ã«åãµã€ãºãæž¡ãããšãã§ããŸãã
æ®å¿µãªãããåã®ãµã€ãºã¯ãã©ãããã©ãŒã ã«ãã£ãŠå€§ããç°ãªãå¯èœæ§ããããŸãããã€ã³ã¿ãŒã®ãµã€ãºã¯æãåçŽãªäŸã§ãã ãã®ãããªåé¡ã解決ããã¹ããŒããªæ¹æ³ã¯ãgetelementptrã¹ããŒãã¡ã³ãã䜿çšããããšã§ã ã
10.3.2ã ã¬ããŒãžã³ã¬ã¯ã¿ãŒã¹ã¿ãã¯ãã¬ãŒã
äžéšã®èšèªã¯ãå€ãã®å Žåãã¬ããŒãžã³ã¬ã¯ã¿ãŒã®ååšããã¯ããŒãžã£ãŒã®å®è£ ã容æã«ããããã«ãã¹ã¿ãã¯ãã¬ãŒã ãæ瀺çã«å¶åŸ¡ããããšãæãã§ããŸãã å€ãã®å Žåããããã®æ©èœãå®è£ ããã«ã¯ãæ瀺çãªã¹ã¿ãã¯ãã¬ãŒã 管çãããåªããæ¹æ³ããããŸãããLLVMã¯å¿ èŠã«å¿ããŠããããµããŒãããŠããŸãã ãããè¡ãã«ã¯ãããã³ããšã³ãã§ã³ãŒããContinuation Passing Styleã«å€æããããŒã«ã³ãŒã«ïŒLLVMããµããŒãïŒã䜿çšããå¿ èŠããããŸãã