GithubHelp home page GithubHelp logo

icyfox168168 / c-- Goto Github PK

View Code? Open in Web Editor NEW

This project forked from delta-in-hub/c--

2.0 0.0 0.0 1.56 MB

A simple C-like compiler for generating executable programs on Windows x86_64 platform

C++ 99.93% C 0.07%

c--'s Introduction

C--

A simple C-like grammar complier,Generate executable files for windows x86_64 platform.

C-- Generate assembly file and then nasm -fwin64 <file.asm> && gcc <file.obj> -o <file.exe>

Requirement

  1. nasm
    1. Download it from https://www.nasm.us/
  2. gcc
    1. I recommend https://jmeubank.github.io/tdm-gcc/
    2. Or any gcc base on MinGW-w64
  3. Add them to Environment variable.Or usec--.exe -n <path> -g <path> to set them as argument.

Build

  1. cd src/ && g++ *.cpp -std=c++17 -Wall -ofast -o c--.exe
  2. Or Double click C--.sln and then ...

Example

>> ./c--.exe -h
Usage: c--.exe [-v] [-d] [-s] [-c] <SourceFile> [-o <TargetFile>] [-n <NasmPath>] [-g <GccPath>]

  -v                       Display compiler version information.
  -d                       Enable Debug Mode.
  -s                       Reserve Assemble File.
  -c                       Reserve Object File.
  -o <TargetFile>          Place the output into <TargetFile>.
  -n <NasmPath>            Set nasm.exe path from <NasmPath>.
  -g <GccPath>             Set gcc.exe path from <GccPath>.

Default:
  Disable Debug Mode.Do not Reserve .asm and .obj File.Place the output into the same folder of <SourceFile>

See https://github.com/Delta-in-hub/C-- for more information

>> ./c--.exe -s ./testCase/in12.txt -o test.exe
>> ./test.exe
a == 10
While Looping
While Looping
While Looping
While Looping
For Looping
For Looping
For Looping
For Looping
For Looping

>> cat test.asm

section .data
arr :
	times 10 dd 0
a :
	dd 12

......

>> cat ./testCase/in12.txt

int arr[10];
int a = 12;
int *p;

void puts(char* str)
{
    /* Reserved Function. Link it to C library later. C:\WINDOWS\System32\msvcrt.dll */
}
int getchar()
{
    /* Reserved Function. Link it to C library later. C:\WINDOWS\System32\msvcrt.dll */
}

int main()
{
    arr[0] = 1+1+1+1+1+2+2+1;
    arr[1] = 10 - 10 + 20 - 2 * 10 + arr[0];
    p = &a;
    *p = 20;
    if(a == arr[0] + arr[1])
    {
        puts("a == 10");
    }
    while(a <= 23)
    {
        puts("While Looping");
        a++;
    }
    for(a = 5; a != 0; a-- )
    {
        puts("For Looping");
    }
    getchar();
    return 0;
}

Notice

C-- did basically a complete front-end, including syntax/semantic analysis.But in back-end,we didn't do much work on code optimization. In object code generation,C-- don't support struct member access and treat all types as int32_t when you referencing variables.

A bit like c89,C-- requires all variables to be declared at the beginning of a function.

Functionputs(char*) and getchar() are both reserved.They will be linked to C library.

Reference

  1. https://github.com/rui314/9cc I learnd a lot from 9cc.Thanks so much.
  2. NASM Tutorial
    1. Using NASM on Windows. Weird shadow space before each call.
  3. x86_64 NASM Assembly Quick Reference ("Cheat Sheet")
  4. x86 and amd64 instruction reference

c--'s People

Contributors

ay-chuya avatar delta-in-hub avatar tokimitsu40 avatar twilightmayfly avatar

Stargazers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.