Cè¨èªã®ã¤ã³ã©ã¤ã³é¢æ°ã«ã¤ãã¦
GCCãå¤ãã®ã³ã³ãã¤ã©ã¼ã¯ï¼Cè¨èªã®æ¨æº(C99)ã«ã¤ã³ã©ã¤ã³é¢æ°ãåãè¾¼ã¾ãããã£ã¨åããã¤ã³ã©ã¤ã³é¢æ°ããµãã¼ããã¦ããããã®ãããã¤ã³ã©ã¤ã³é¢æ°ã®å®ç¾©ã¯æ§ã
ã§ï¼åãæ¸å¼ã§ãåã³ã³ãã¤ã©ã§æåãç°ãªãå ´åãããã
ä»åã¯ï¼ã¤ã³ã©ã¤ã³é¢æ°ã®å®ç¾©ãã¾ã¨ããããããåºåãããã¢ã»ã³ãã©ã³ã¼ããæ¯ã¹ã¦ã¿ãã
æ¦è¦
å é ã« inline ã¨ããè¨èãä»ãã¦é¢æ°ã宣è¨ããã¨ï¼ã³ã³ãã¤ã©ã¼ã¯ããããã³ãã«ã³ã¼ããã¤ã³ã©ã¤ã³åââé¢æ°ã®ã³ã¼ããå¼åºãå ã«å±éãããããã«ããï¼é¢æ°å¼ã³åºãã®ãªã¼ãã¼ããããåãé¤ããå®è¡ãæ©ããªãã
inline ã¯ã³ã³ãã¤ã©ã«å¯¾ãããã³ãã§ãã£ã¦æ示ã§ã¯ãªãããï¼æ§ã ãªçç±ãããã³ããç¡è¦ããï¼ã¤ã³ã©ã¤ã³åãããå®éã®é¢æ°ã¨ãã¦åºåãããå ´åããããä¾ãã°ï¼ãã®é¢æ°ã®ã¢ãã¬ã¹ãåç §ãããå ´åãï¼é¢æ°å®ç¾©å ã®å帰å¼åºããªã©ã¯ã¤ã³ã©ã¤åã§ããªããï¼ã³ã³ãã¤ã©ã¼ã®æé©åãç¡å¹ã«ããå ´åã¤ã³ã©ã¤ã³åã¯è¡ãããªãã
GCCã®ã¤ã³ã©ã¤ã³é¢æ°
GCCã§ã¯ inlineï¼static inlineï¼ãã㦠extern inline ãå®ç¾©ããã¦ããã
static inline
é¢æ°ã static inline ã¨å®£è¨ããã¨ï¼é¢æ°ããã¹ã¦ã¤ã³ã©ã¤ã³åãããã°ï¼ãã®é¢æ°èªä½ã¯åºåãããªãã
次ã®ãããªãµã³ãã«ã³ã¼ããç¨æããï¼
static inline void outb(unsigned short port, unsigned char val) { __asm__ __volatile__ ("out dx, al" : : "d" (port), "a" (val)); } int fnc() { outb(0x20, 0x11); }
ããã次ã®ããã«ã³ã³ãã¤ã«ããï¼
gcc -O2 -fno-ident -finhibit-size-directive -fomit-frame-pointer -fcall-used-ebx -masm=intel -S -c main.c
åºåãããã³ã¼ãã¯æ¬¡ã®éãã§ããï¼
.file "main.c" .intel_syntax noprefix .text .p2align 4,,15 .globl fnc .type fnc, @function fnc: mov eax, 17 mov edx, 32 #APP # 2 "main.c" 1 out dx, al # 0 "" 2 #NO_APP ret .section .note.GNU-stack,"",@progbits
outb()ãã¤ã³ã©ã¤ã³åããï¼é¢æ°ãfnc()ã²ã¨ã¤ããæ®ã£ã¦ãªããã¨ãåããã
inline
é¢æ°ã inline ã¨å®£è¨ããã¨ï¼static ã§ãªãããï¼ã³ã³ãã¤ã©ã¯ä»ã®ã½ã¼ã¹ãã¡ã¤ã«ããã®å¼åºãããããã¨ãæ³å®ããªããã°ãªããªãããã®ããï¼é¢æ°ããã¹ã¦ã¤ã³ã©ã¤ã³åããã¦ãï¼å¸¸ã«é¢æ°èªä½ã¯åºåãããã
åºåãããã³ã¼ãã¯æ¬¡ã®éãã§ããï¼
.file "main.c" .intel_syntax noprefix .text .p2align 4,,15 .globl outb .type outb, @function outb: movzx edx, WORD PTR [esp+4] movzx eax, BYTE PTR [esp+8] #APP # 2 "main.c" 1 out dx, al # 0 "" 2 #NO_APP ret .p2align 4,,15 .globl fnc .type fnc, @function fnc: mov eax, 17 mov edx, 32 #APP # 2 "main.c" 1 out dx, al # 0 "" 2 #NO_APP ret .section .note.GNU-stack,"",@progbits
outb()ã¯ã¤ã³ã©ã¤ã³åããã¦ãããï¼é¢æ°outb()ãåºåããã¦ããã
extern inline
é¢æ°ã extern inline ã¨å®£è¨ããã¨ï¼å¤é¨åç
§ã«ãªãããï¼å¸¸ã«é¢æ°èªä½ã¯åºåãããªãã
åºåãããã³ã¼ãã¯æ¬¡ã®éãã§ããï¼
.file "main.c" .intel_syntax noprefix .text .p2align 4,,15 .globl fnc .type fnc, @function fnc: mov eax, 17 mov edx, 32 #APP # 2 "main.c" 1 out dx, al # 0 "" 2 #NO_APP ret .section .note.GNU-stack,"",@progbits
ããã§ã¯ï¼static inlineã¨åãã³ã¼ããåºåãããã
ããï¼é¢æ°ãã¤ã³ã©ã¤ã³åãããªãå ´åã¯ï¼å¤é¨åç §ã«ãªãããï¼æ¬¡ã®ãããªé¢æ°æ¬ä½ã®ã³ãã¼ãå¤é¨ãã¡ã¤ã«ã«ç¨æããå¿ è¦ãããï¼
void outb(unsigned short port, unsigned char val) { __asm__ __volatile__ ("out dx, al" : : "d" (port), "a" (val)); }
C99ã®ã¤ã³ã©ã¤ã³é¢æ°
C99ã§ã¯GCCã®ä¸ã¤ã®ã¤ã³ã©ã¤ã³é¢æ°ãåãè¾¼ã¾ããã
ãã¯ããã static inline ãæ¨å¥¨
åã®å®å ¨æ§ãããããã©ã¼ãããã®å¶éãç¡ããã static inline é¢æ°ã¯ãã¯ãããããã£ã¨æ¨å¥¨ããã¦ããã