Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
52 commits
Select commit Hold shift + click to select a range
29458c8
Initialization to rewrite parser
falgon Dec 2, 2020
2cb3e32
Merge branch 'master' into monadic
falgon Dec 3, 2020
a6e3f76
Transition to Monadic Parser: Calculation of four arithmetic operations
falgon Dec 7, 2020
a64a2ca
Changed to use makeExprParser
falgon Dec 9, 2020
2956d8e
Implemented some binary operators, untyped (all int) variables and re…
falgon Dec 12, 2020
01ed216
Module movement
falgon Dec 12, 2020
eefd2b3
add if statement
falgon Dec 13, 2020
0d8526e
add while statement
falgon Dec 13, 2020
bfde469
add for statement
falgon Dec 13, 2020
0bbd3fb
add compound statement
falgon Dec 13, 2020
df42919
add function call
falgon Dec 13, 2020
b89b27b
add function definition
falgon Dec 13, 2020
d4d0845
add unary * and &
falgon Dec 13, 2020
0ce3ecc
Add declaration of local variable (int only)
falgon Dec 20, 2020
5b285f4
Changed to be able to type in function parameters
falgon Dec 20, 2020
bef7cbf
Changed function declarations and definitions to be typed
falgon Dec 20, 2020
9961594
Add pointer type
falgon Dec 20, 2020
1c8baeb
Add pointer calculation
falgon Dec 20, 2020
1fce7ac
Add sizeof
falgon Dec 20, 2020
22f202c
Allowed parsing of array declarations with number of elements
falgon Dec 22, 2020
f73091f
Add an array test
falgon Dec 22, 2020
a4e61fe
Add incomplete array type parser
falgon Dec 24, 2020
2a83c86
Add index access
falgon Dec 26, 2020
392ce1b
Add global variable and char tests
falgon Dec 27, 2020
54f9119
Add string literals
falgon Dec 27, 2020
a74400b
Add bits and boolean operators
falgon Dec 27, 2020
b5924ca
Add statement expression
falgon Dec 28, 2020
12e0a09
Allow no body for statements
falgon Dec 28, 2020
02f0344
Add a conditional operator
falgon Dec 28, 2020
95b5b83
Add assignment operators
falgon Dec 28, 2020
84c7e45
Add the pre/post increment and decrement operator
falgon Dec 28, 2020
4023eea
Add the break statement
falgon Dec 28, 2020
511afeb
Add the continue statement
falgon Dec 28, 2020
ae48ab8
Add the switch, case and default statement
falgon Dec 28, 2020
56a65bf
Add the goto statement
falgon Dec 28, 2020
db52236
Add tests
falgon Dec 28, 2020
a44188f
refactoring: Separated parser combinator for GNU extensions
falgon Dec 28, 2020
87711a4
Allow array initialization list
falgon Jan 5, 2021
e157e80
refactoring
falgon Jan 5, 2021
bce7196
Changed so that unit test files can be executed separately
falgon Jan 7, 2021
d6addc1
Allow array initialization with strings
falgon Jan 10, 2021
f584afc
Allow void function
falgon Jan 11, 2021
45f0bf0
refactoring unit tests
falgon Jan 11, 2021
25041d1
Add warning text management
falgon Jan 12, 2021
8348d20
Add _Alignof
falgon Jan 14, 2021
7a9c0ec
Add abstract declarators
falgon Jan 16, 2021
edd70e2
Add cast
falgon Jan 16, 2021
d09c968
Partially supported sizeof function
falgon Jan 26, 2021
7fe7783
Allow sizeof for function (GNU extension)
falgon Feb 7, 2021
7235a9c
Supports abstract declarator for functions
falgon Feb 11, 2021
3248a8c
Replace Megaparsec with Parsec compatibility layer
falgon Mar 26, 2026
c2e3ed0
Implement multi-input output flow and type compatibility fixes
falgon Apr 1, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,27 @@
.stack-work
.stack-root*/
.cabal-local/
.cabal-home/
.cabal-store/
.cabal-sandbox/
.codex-home/
.home/
.serena/
dist-newstyle/
dist-newstyle-review*/
dist-newstyle-sandbox/
dist-newstyle-local/
dist-review*/
a.out
.DS_Store
*.hi
*.hi-boot
*.o
*.o-boot
*.dyn_hi
*.dyn_o
tmp*
.tmp*
*.swp
bench_report.html
docs/
Expand Down
12 changes: 9 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,14 @@ $ gcc -no-pie t.s -o out
For one liner:

```sh
$ echo 'int printf(); int main() { printf("hello world!\n"); }' | stack exec htcc -- /dev/stdin | gcc -xassembler -no-pie -o out -
$ echo 'int printf(); int main() { printf("hello world!\n"); }' | stack exec htcc -- /dev/stdin | gcc -x assembler -no-pie -o out -
```

When using `-r`/`--run-asm`, htcc uses the driver selected by
`$HTCC_ASSEMBLER` for both assembly and linking.
On hosts where `gcc` points to clang, set `HTCC_ASSEMBLER` to a
GNU-compatible compiler driver before running htcc.

## AST diagram generation

htcc has the ability to visualize ASTs built from loaded C code.
Expand Down Expand Up @@ -167,8 +172,9 @@ The implementation description is available in [here](https://falgon.github.io/h
## Specification and Requirements

htcc outputs x86_64 assembly according to System V ABI [[2]](#cite2) and
[GCC 7.4.0](https://gcc.gnu.org/onlinedocs/7.4.0/) is used for assemble.
Perhaps a newer version of GCC will work, but not checked currently.
[GCC 7.4.0](https://gcc.gnu.org/onlinedocs/7.4.0/) is used for assemble.
Perhaps a newer version of GCC will work, but not checked currently. When
assembling via `-r`, choose the driver with `HTCC_ASSEMBLER`.

## About emoji of commit messages

Expand Down
1,518 changes: 1,434 additions & 84 deletions app/Main.hs

Large diffs are not rendered by default.

7 changes: 7 additions & 0 deletions docker/scripts/test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/bin/bash
find /htcc_work -name "*.s" -type f |\
while read fname; do
gcc -xassembler -no-pie -o "$(dirname $fname)/$(basename $fname '.s').o" $fname
echo ">>>>> $fname"
./$(dirname $fname)/$(basename $fname ".s").o
done
4 changes: 2 additions & 2 deletions docker/test.dhall
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ let htccService =
⫽ { image = Some "roki/htcc_test:1.0.0"
, command = Some
( types.StringOrList.String
"/bin/sh -c 'gcc -no-pie -o spec /htcc_work/spec.s && ./spec'"
"/bin/bash /htcc_work/scripts/test.sh"
)
, volumes = Some [ "/tmp/htcc:/htcc_work" ]
, volumes = Some [ "/tmp/htcc:/htcc_work", "./docker/scripts:/htcc_work/scripts" ]
, build = Some
( types.Build.Object
{ context = "."
Expand Down
65 changes: 49 additions & 16 deletions htcc.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ cabal-version: 1.12
--
-- see: https://github.com/sol/hpack
--
-- hash: 22bacaccc5bc8617817eb1c982386f1b7ce9a77e1928a9f47181e1dcd7a27d2a
-- hash: 850b9848b962537995097e4ec0c7bc1c88f136695936efa9068db58eece3242d

name: htcc
version: 0.0.0.1
Expand Down Expand Up @@ -63,14 +63,28 @@ library
Htcc.CRules.Types.CType
Htcc.CRules.Types.StorageClass
Htcc.CRules.Types.TypeKind
Htcc.Output
Htcc.Parser
Htcc.Parser.AST
Htcc.Parser.AST.Core
Htcc.Parser.AST.DeduceKind
Htcc.Parser.AST.Type
Htcc.Parser.AST.Var
Htcc.Parser.AST.Var.Init
Htcc.Parser.ConstructionData
Htcc.Parser.Combinators
Htcc.Parser.Combinators.BasicOperator
Htcc.Parser.Combinators.ConstExpr
Htcc.Parser.Combinators.Core
Htcc.Parser.Combinators.Decl
Htcc.Parser.Combinators.Decl.Declarator
Htcc.Parser.Combinators.Decl.Spec
Htcc.Parser.Combinators.GNUExtensions
Htcc.Parser.Combinators.Keywords
Htcc.Parser.Combinators.ParserType
Htcc.Parser.Combinators.Program
Htcc.Parser.Combinators.Type
Htcc.Parser.Combinators.Type.Core
Htcc.Parser.Combinators.Type.Utils
Htcc.Parser.Combinators.Utils
Htcc.Parser.Combinators.Var
Htcc.Parser.ConstructionData.Core
Htcc.Parser.ConstructionData.Scope
Htcc.Parser.ConstructionData.Scope.Enumerator
Expand All @@ -80,16 +94,12 @@ library
Htcc.Parser.ConstructionData.Scope.Typedef
Htcc.Parser.ConstructionData.Scope.Utils
Htcc.Parser.ConstructionData.Scope.Var
Htcc.Parser.Parsing
Htcc.Parser.Parsing.Core
Htcc.Parser.Parsing.Global
Htcc.Parser.Parsing.Global.Function
Htcc.Parser.Parsing.Global.Var
Htcc.Parser.Parsing.StmtExpr
Htcc.Parser.Parsing.Type
Htcc.Parser.Parsing.Typedef
Htcc.Parser.Utils
Htcc.Parser.Utils.Core
Text.Megaparsec
Text.Megaparsec.Char
Text.Megaparsec.Char.Lexer
Text.Megaparsec.Debug
Htcc.Tokenizer
Htcc.Tokenizer.Core
Htcc.Tokenizer.Token
Expand All @@ -114,20 +124,26 @@ library
, cond
, containers
, deepseq
, directory
, diagrams-contrib
, diagrams-lib
, diagrams-svg
, extra
, filepath
, parsec
, monad-finally
, monad-loops
, mono-traversable
, mtl
, natural-transformation
, optparse-applicative
, parser-combinators
, safe
, split
, text
, transformers
, unix
, utf8-string
default-language: Haskell2010

executable htcc
Expand All @@ -136,7 +152,7 @@ executable htcc
Paths_htcc
hs-source-dirs:
app
ghc-options: -threaded -rtsopts -with-rtsopts=-N -Wall -Werror -O2
ghc-options: -threaded -rtsopts -with-rtsopts=-N -Wall -O2
build-depends:
ansi-wl-pprint
, base >=4.7 && <5
Expand All @@ -149,27 +165,38 @@ executable htcc
, diagrams-svg
, directory
, extra
, filepath
, htcc
, process
, parsec
, monad-finally
, monad-loops
, mono-traversable
, mtl
, natural-transformation
, optparse-applicative
, parser-combinators
, safe
, split
, template-haskell
, text
, transformers
, unix
, utf8-string
default-language: Haskell2010

test-suite htcc-test
type: exitcode-stdio-1.0
main-is: Spec.hs
other-modules:
Tests.ComponentsTests
Tests.ComponentsTests.AsmOutput
Tests.ComponentsTests.Parser.Combinators
Tests.SubProcTests
Tests.Test1
Tests.Test2
Tests.Test3
Tests.SubProcTests.AsmOutput
Tests.SubProcTests.LinkFuncRet
Tests.SubProcTests.LinkFuncStdOut
Tests.SubProcTests.StatementEqual
Tests.Utils
Paths_htcc
hs-source-dirs:
Expand All @@ -196,19 +223,22 @@ test-suite htcc-test
, hspec-contrib
, hspec-core
, htcc
, parsec
, monad-finally
, monad-loops
, mono-traversable
, mtl
, natural-transformation
, optparse-applicative
, parser-combinators
, process
, safe
, split
, text
, time
, transformers
, turtle
, unix
, utf8-string
default-language: Haskell2010

Expand All @@ -231,14 +261,17 @@ benchmark criterion
, diagrams-svg
, extra
, htcc
, parsec
, monad-finally
, monad-loops
, mono-traversable
, mtl
, natural-transformation
, optparse-applicative
, parser-combinators
, safe
, split
, text
, transformers
, utf8-string
default-language: Haskell2010
23 changes: 22 additions & 1 deletion package.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ dependencies:
- containers
- bytestring
- deepseq
- directory
- safe
- filepath
- mtl
- monad-finally
- mono-traversable
Expand All @@ -46,6 +48,10 @@ dependencies:
- diagrams-lib
- natural-transformation
- optparse-applicative
- parsec
- parser-combinators
- unix
- utf8-string

library:
source-dirs: src
Expand All @@ -59,16 +65,30 @@ executables:
- -rtsopts
- -with-rtsopts=-N
- -Wall
- -Werror
#- -Werror
- -O2
dependencies:
- htcc
- directory
- filepath
- process
- template-haskell
- unix

tests:
htcc-test:
main: Spec.hs
source-dirs: test
other-modules:
- Tests.ComponentsTests
- Tests.ComponentsTests.AsmOutput
- Tests.ComponentsTests.Parser.Combinators
- Tests.SubProcTests
- Tests.SubProcTests.AsmOutput
- Tests.SubProcTests.LinkFuncRet
- Tests.SubProcTests.LinkFuncStdOut
- Tests.SubProcTests.StatementEqual
- Tests.Utils
ghc-options:
- -threaded
- -rtsopts
Expand All @@ -90,6 +110,7 @@ tests:
- dhall-json
- dhall-yaml
- process
- unix
- utf8-string

benchmarks:
Expand Down
9 changes: 5 additions & 4 deletions src/Htcc/Asm.hs
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,17 @@ module Htcc.Asm (
casm
) where

import Data.Tuple.Extra (uncurry3)

import Data.Bits (Bits)
import Htcc.Asm.Generate
import qualified Htcc.Asm.Intrinsic.Operand as O
import qualified Htcc.Asm.Intrinsic.Structure.Internal as SI
import qualified Htcc.Asm.Intrinsic.Structure.Section.Text.Instruction as TI
import Htcc.Parser (ASTs)
import qualified Htcc.Parser.ConstructionData.Scope.Function as PF
import Htcc.Parser.ConstructionData.Scope.Var (GlobalVars,
Literals)
import Htcc.Utils (uncurry4)

-- | Generate full assembly code from string of C source code
casm :: (O.IsOperand i, TI.UnaryInstruction i, TI.BinaryInstruction i, Integral i) => (ASTs i, GlobalVars i, Literals i) -> IO ()
casm = SI.runAsm . uncurry3 casm'
casm :: (Bits i, Read i, Show i, Ord i, O.IsOperand i, TI.UnaryInstruction i, TI.BinaryInstruction i, Integral i) => (ASTs i, GlobalVars i, Literals i, PF.Functions i) -> IO ()
casm = SI.runAsm . uncurry4 casm'
Loading