PCC 的使用

編譯器設計

編譯器簡介

高階語言

語法理論

剖析器

語意理論

符號表

直譯器

型態系統

中間碼

目標語言

最佳化

錯誤處理

進階議題

原始碼下載

程式實作

C 語言

案例研究

JavaScript

V8

Lua

Oberon

NeoPascal

pcc

tcc

gcc

C--

Lex

YACC

AntLR

LLVM

CLang

訊息

相關網站

參考文獻

最新修改

簡體版

English

程式範例

#include <stdio.h>

#define max(a,b) (a>b?a:b)

int main()
{
    int x = max(3,5);
    printf("max(3,5)=%d\n", x);

    int s = sum(10);
    printf("sum(10)=%d\n", s);
    return 0;
}

int sum(int n) {
    int i, s = 0;
    for (i=1; i<=n; i++)
        s += i;
    return s;
}

編譯執行

ccc@ccc-PC ~
$ pcc -v sum.c -o sum.exe
pcc 1.1.0 for win32, gmcgarry@pcc.ludd.ltu.se
"C:\pcc\libexec\cpp.exe" "-v" "-D__PCC__=1" "-D__PCC_MINOR__=1" "-D__PCC_MINORMI
NOR__=0" "-D__VERSION__=\"pcc 1.1.0 for win32, gmcgarry@pcc.ludd.ltu.se\"" "-D__
SCHAR_MAX__=127" "-D__SHRT_MAX__=32767" "-D__INT_MAX__=0x7fffffff" "-D__LONG_MAX
__=0x7fffffff" "-D__LONG_LONG_MAX__=0x7fffffffffffffffLL" "-D__REGISTER_PREFIX__
=" "-D__USER_LABEL_PREFIX__=" "-DWIN32" "-D__MSVCRT__" "-D__MINGW32__" "-D__STDC
_ISO_10646__=200009L" "-D__WCHAR_TYPE__=short unsigned int" "-D__SIZEOF_WCHAR_T_
_=2" "-D__WCHAR_MAX__=65535U" "-D__WINT_TYPE__=unsigned int" "-D__SIZE_TYPE__=un
signed long" "-D__PTRDIFF_TYPE__=long int" "-D__SIZEOF_WINT_T__=4" "-D__i386__"
"-S" "C:\pcc\include\pcc" "-S" "C:\pcc\include" "-S" "C:\pcc\lib\i386-win32\1.1.
0\include" "sum.c" "C:\Users\ccc\AppData\Local\Temp\ctm2221.tmp"
cpp: pcc 1.1.0 for win32, gmcgarry@pcc.ludd.ltu.se
"C:\pcc\libexec\ccom.exe" "C:\Users\ccc\AppData\Local\Temp\ctm2221.tmp" "C:\User
s\ccc\AppData\Local\Temp\ctm2220.tmp"
"yasm.exe" "-p" "gnu" "-f" "win32" "-o" "C:\Users\ccc\AppData\Local\Temp\ctm2251
.tmp" "C:\Users\ccc\AppData\Local\Temp\ctm2220.tmp"
"ld.exe" "-v" "-o" "sum.exe" "C:\pcc\lib\crt2.o" "C:\Users\ccc\AppData\Local\Tem
p\ctm2251.tmp" "-LC:\pcc\lib\i386-win32\1.1.0\lib" "-LC:\pcc\lib" "-lmoldname" "
-lmingwex" "-lmingw32" "-lpcc" "-lmsvcrt" "-luser32" "-lkernel32"
GNU ld version 2.17.50 20060824

ccc@ccc-PC ~
$ pcc -E sum.c -o sum.i

ccc@ccc-PC ~
$ pcc -E sum.c -o sum.i

ccc@ccc-PC ~
$ pcc -S sum.c -o sum.s

ccc@ccc-PC ~
$ pcc -c sum.c -o sum.o

ccc@ccc-PC ~
$ pcc sum.c -o sum

ccc@ccc-PC ~
$ sum
max(3,5)=5
sum(10)=55

ccc@ccc-PC ~
$

輸出檔案

檔案:sum.i

int main()
{
    int x = (3>5?3:5);
    printf("max(3,5)=%d\n", x);

    int s = sum(10);
    printf("sum(10)=%d\n", s);
    return 0;
}

int sum(int n) {
    int i, s = 0;
    for (i=1; i<=n; i++)
        s += i;
    return s;
}

檔案:sum.s

    .section .rodata
L164:
    .ascii "max(3,5)=%d\012\0"
L166:
    .ascii "sum(10)=%d\012\0"
    .text
    .align 4
    .globl _main
_main:
    pushl %ebp
    movl %esp,%ebp
    subl $12,%esp
L161:
L163:
    movl $5,-4(%ebp)
    pushl -4(%ebp)
    pushl $L164
    call _printf
    addl $8, %esp
    pushl $10
    call _sum
    addl $4, %esp
    movl %eax,-8(%ebp)
    pushl -8(%ebp)
    pushl $L166
    call _printf
    addl $8, %esp
    movl $0,-12(%ebp)
    jmp L162
L162:
    movl -12(%ebp),%eax
    leave
    ret
    .align 4
    .globl _sum
_sum:
    pushl %ebp
    movl %esp,%ebp
    subl $12,%esp
L168:
L170:
    movl $0,-8(%ebp)
    movl $1,-4(%ebp)
L173:
    movl 8(%ebp),%eax
    cmpl %eax,-4(%ebp)
    jg L172
    movl -4(%ebp),%eax
    addl %eax,-8(%ebp)
L171:
    incl -4(%ebp)
    jmp L173
L172:
    movl -8(%ebp),%eax
    movl %eax,-12(%ebp)
    jmp L169
L169:
    movl -12(%ebp),%eax
    leave
    ret
    .ident "PCC: pcc 1.1.0 (win32)"

Facebook

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 License