{"id":22415140,"url":"https://github.com/abertschi/beancd-lang","last_synced_at":"2026-02-28T10:03:41.780Z","repository":{"id":74673553,"uuid":"241903525","full_name":"abertschi/beancd-lang","owner":"abertschi","description":"The beancd programming language","archived":false,"fork":false,"pushed_at":"2020-02-20T14:57:30.000Z","size":3126,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-27T04:17:03.669Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":null,"has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/abertschi.png","metadata":{"files":{"readme":"README.org","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-02-20T14:22:00.000Z","updated_at":"2020-07-17T14:56:27.000Z","dependencies_parsed_at":"2023-07-29T03:16:06.625Z","dependency_job_id":null,"html_url":"https://github.com/abertschi/beancd-lang","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/abertschi/beancd-lang","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/abertschi%2Fbeancd-lang","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/abertschi%2Fbeancd-lang/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/abertschi%2Fbeancd-lang/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/abertschi%2Fbeancd-lang/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/abertschi","download_url":"https://codeload.github.com/abertschi/beancd-lang/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/abertschi%2Fbeancd-lang/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":29929601,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-02-28T09:58:13.507Z","status":"ssl_error","status_checked_at":"2026-02-28T09:57:57.047Z","response_time":90,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.6:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-12-05T15:11:21.171Z","updated_at":"2026-02-28T10:03:41.736Z","avatar_url":"https://github.com/abertschi.png","language":null,"funding_links":[],"categories":[],"sub_categories":[],"readme":"#+BEGIN_SRC \n          _                              _ \n         | |                            | |\n         | |__   ___  __ _ _ __   ___ __| |\n         | '_ \\ / _ \\/ _` | '_ \\ / __/ _` |\n   THE   | |_) |  __/ (_| | | | | (_| (_| |  LANGUAGE\n         |_.__/ \\___|\\__,_|_| |_|\\___\\__,_|\n\n                 \n            ---- the beancd language ----\n#+END_SRC\n\n#+TITLE: The beancd programming language\n\n-beancd- (pronounced beancode) is a simple yet complete compiler for\nthe -beancd language-, a java-like object-oriented programming\nlanguage with an IA-32 asm backend.\n\nThis is a learning project to gain practical experience in designing\nand implementing a compiler (5k loc). I built it for the course Compiler Design\ntaught at ETH Zurich. Beancd covers the main steps in a compiler\npipeline including;\n\n- Lexical analysis\n- Syntax analysis\n- AST intermediate representation\n- Semantical analysis\n- AST optimizations, simple CFG and Data-Flow analysis\n- IA-32 assembly backend\n\nBeancd is writtin in Java and compiles the beancd language to IA-32 assembly.\n\n* Language Features\nSome of beancds main language feature include;\n\n- Object oriented\n- Single inheritance\n- Statically typed\n- static scoping: global, class, method scope\n- support for expressions and statements\n- no constructors, no abstract classes\n- supports primitive datatypes boolean, and int\n- Arrays are not co-variant, super of all Arrays is Object\n- built-in methods for read(), write(expr) and writeln()\n- runtime errors for: downcast, array store, out-of-bound, null\n  pointer\n- syscalls are delegated to libc functions\n\n* tldr; Hello World aka print 1337\ninput: beancd\n#+BEGIN_SRC java\nclass Main {\n    void main() {\n        write(1337);\n    }\n}\n#+END_SRC\n\noutput: ia-32\n#+BEGIN_SRC asm\n# === WRITELN CONFIG SECTION\n.section .data\nSTR_NL:\n  .string \"\\n\"\nSTR_D:\n  .string \"%d\"\n\n# === VTABLE SECTION\n.section .data\n___vtable_boolean:\n  .long 0xB00B5BAE                               #     0: ___super_vptr\n\n___vtable_Object:\n  .long 0xB00B5BAE                               #     0: ___super_vptr\n\n___vtable_Main:\n  .long ___vtable_Object                         #     0: ___super_vptr\n  .long ___Main_main                             #     4: main\n\n___vtable_int:\n  .long 0xB00B5BAE                               #     0: ___super_vptr\n\n.section .text\n__rte_array_oob:\n  subl $12, %esp\n  movl $3, 0(%esp)\n  calll exit\n  addl $12, %esp\n\n__rte_null:\n  subl $12, %esp\n  movl $4, 0(%esp)\n  calll exit\n  addl $12, %esp\n\n__rte_array_size:\n  subl $12, %esp\n  movl $5, 0(%esp)\n  calll exit\n  addl $12, %esp\n\n__rte_divby0:\n  subl $12, %esp\n  movl $7, 0(%esp)\n  calll exit\n  addl $12, %esp\n\n# === MAIN SECTION\n.section .text\n.globl main\nmain:\n  pushl  %ebp\n  movl %esp, %ebp\n  subl $12, %esp\n  # Bootstrap baby, bootstrap\n  subl $16, %esp\n  movl $8, %esi\n  subl $12, %esp\n  movl %esi, 4(%esp)\n  movl $1, 0(%esp)\n  call calloc\n  movl %eax, %edi\n  add $12, %esp\n   # ref to vtable setup, ref: $___vtable_Main\n  movl $___vtable_Main, 0(%edi)\n  # object meta data setup\n  movl $0, 4(%edi)\n  subl $12, %esp # 12 byte padding before call\n  movl %edi, 0(%esp)\n  calll ___Main_main\n  addl $12, %esp\n  addl $16, %esp\n  addl $12, %esp\n  popl %ebp\n  xor %eax, %eax\n  retl\n\n# class Main {...}\n  # void main(...) {...}\n  # === BEGIN Method Main#main\n  # callee stack layout for ___Main_main\n  #    8(%EBP): TARGET_OBJ      (  4 bytes) n/a\n  #    4(%EBP): RETURN_ADDR     (  4 bytes) n/a\n  #    0(%EBP): OLD_BP          (  4 bytes) n/a\n  #   -4(%EBP): PADDING16       ( 12 bytes) n/a\n___Main_main:\n  pushl  %ebp\n  movl %esp, %ebp\n  subl $12, %esp\n  movl $1337, %edi\n  subl $0, %esp\n  sub $12, %esp\n  movl %edi, 4(%esp)\n  movl $STR_D, 0(%esp)\n  call printf\n  add $12, %esp\n  addl $0, %esp\n  # method clean up\n  movl %ebp, %esp\n  popl %ebp\n  ret\n#+END_SRC\n\n* Specification\nBeancd is a reference implementation of the language specification\nJavaLi. JavaLi was specified for the course Compiler Design taught by\nProf. Thomas Gross at ETH Zurich (2017).\n\nSee the specification at TODO\n\n* Design Notes\n** Stack Layout\nUpon a function call a stack frame is allocated. Stack frames headers have\nthe following layout; \n\n#+BEGIN_SRC\n# +00: RETURN_VAL      \n# -04  METHOD_ARG      right most argument first\n# -08: METHOD_ARG      another argument\n# -12: TARGET_OBJ      reference to this object\n# -16: RETURN_ADDR     \n# -20: OLD_BP          \n# -24: LOCAL_VAR       for each local variable, one such entry exists\n# -28: PADDING16       padding for 16 byte alignment\n\n#+END_SRC\n- Padding entries ensure 16 byte alignment (required to be compatible\n  with IA-32 linux ABI to call gcc functions)\n- The ~RETURN_VAL~  entry is only present if the function has a return\n  value.\n- ~LOCAL_VAR~ refers to local (stack) variables declared in the scope of a\n  method.\n\nUpon a new stack frame ~%EBP~ is pushed on the stack (referred as\n~OLD_BP~) and ~%ESP~ is stored in ~%EBP~. \n\n** Memory Layout\nBeancd supports boolean, integer primitive data types, class\ndata types, and array data types of primitive or class data types.\n\n- integer :: 4 bytes\n- boolean :: 4 bytes (simplifies implementation)\n- pointer :: 4 bytes\n- classes :: 8 byte header\n- arrays :: 16 byte header\n\n*** Classes\nClasses have a memory footprint of at least 8 bytes.\n\n**** Class Header\n#+BEGIN_SRC \n+00: pointer to vtable\n+04: meta data\n#+END_SRC\n\n*** Arrays\nArrays have a memory footprint of at least 16 bytes.\n\n**** Array Header\n#+BEGIN_SRC\n+00: pointer to vtable\n+04: meta data\n+08: number of elements\n+12: element size\n#+END_SRC\n\n** Virtual Tables\nVirtual tables are generated in the assembly prologue\nfor each primitive and class data type. The first 4 bytes of a\nvirtual table contains a reference to the super type. These references\nare used for inheritance. Primitive data types do not have a super\ntype which is why the magic number ~0xB00B5BAE~ is used to indicate\nno super type. The class ~Object~ does not have a super type either and also\nuses the same magic number.\n\n\nAll classes (including arrays) are sub-classes of the special class ~Object~. In the\nvtable dump below, class ~___vtable_Main~ contains a reference to\nthe virtual table of special class ~Object~.\n\n#+BEGIN_SRC asm\n.data\n___vtable_boolean:\n  .long 0xB00B5BAE                               #     0: ___super_vptr       \n\n___vtable_Object:\n  .long 0xB00B5BAE                               #     0: ___super_vptr       \n\n___vtable_Main:\n  .long ___vtable_Object                         #     0: ___super_vptr       \n  .long ___Main_myFunction                       #     4: myFunction          \n  .long ___Main_main                             #     8: main                \n\n___vtable_int:\n  .long 0xB00B5BAE                               #     0: ___super_vptr\n\n#+END_SRC\n\n** Runtime Error Detection\nThe assembly prologue also contains implementations of runtime errors.\nRuntime error labels are prefixed with ~__rte_~ (as in Run Time\nError). Runtime errors abort execution with an error code and call \nthe glibc function ~exit~.\n\n*** Runtime Assembly\n#+BEGIN_SRC asm\n    .text \n__rte_array_oob:\n    subl  $12, %esp\n    movl  $3, 0(%esp)\n    calll _exit\n    addl  $12, %esp\n__rte_null:\n    subl  $12, %esp\n    movl  $4, 0(%esp)\n    calll _exit\n    addl  $12, %esp\n__rte_array_size:\n    subl  $12, %esp\n    movl  $5, 0(%esp)\n    calll _exit\n    addl  $12, %esp\n__rte_divby0:\n    subl  $12, %esp\n    movl  $7, 0(%esp)\n    calll _exit\n    addl  $12, %esp\n#+END_SRC\n\n*** Implemented Errors\n- Error 01 :: Invalid downcast\n- Error 02 :: Invalid array allocation\n- Error 03 :: Array out of bounds\n- Error 04 :: Null pointer\n- Error 05 :: Invalid array size on allocation\n- Error 12 :: Division or Modulo 0.\n\n* Optimizations\n** General Assembly Optimizations\n- For binary expressions, fewer assembly instructions are generated if\n  one or both of the two arguments can be determined at compile time.\n  Specifically, the fact that the instruction ~cmpl~ allows one argument to be an\n  immediate value is exploited.\n- A load of a constant value (Int/Boolean) is omitted and immediate\n  values are used instead where possible.\n- Nullchecks are not performed for ThisRef nodes.\n- Fewer instructions for branching between CfgCodeGenerator blocks.\n- If arguments for method calls can be determined at compile time,\n  immediate values are pushed onto stack directlly without moves into\n  registers.\n** ConstantFoldingOpt.java\n- Uses DataFlowAnalysis for constant propagination.\n** ArithmeticPropertyOpt.java\n- Exploits arithmetic properties in multiplications or modulo operations.\n  For instance, a multiplication by 0 is always zero. In case the\n  other argument is not a method call (with side effects), the\n  expression is evaluated to 0 at compile time.\n\n** BinaryOperationOpt.java\n- Exploits commutativity of multiplication and addition if constants\n  are involved.\n\n** StaticConditionOpt.java\n- Removes conditions and branching if condition value can be determined\n  at compile time.\n\n** VarOverwriteOpt.java\n- Removes assign statements of variables whose values are not used and\n  reassigned afterwards.\n\n** EmptyBlockCleanup.java\n- Removes empty/nop cfg graph nodes.\n\n** Further improvements (not enabled/ not implemented)\n*** InlineMethodAnalysis.java\n- Removes method invocation if method returns constant values and replaces \n  invocation by those values. This optimization is currently not\n  enabled as it needs further testing and more advanced\n  implementations for stronger optimizations.\n\n*** Implement null check analysis\n- Remove unecessary null checks, not implemented\n\n*** Remove assignments in Loops whose value does not dependent on iteration\n- Not implemented\n* More Examples\n*** Source Language\n#+BEGIN_SRC java\n  class Main {\n    void main() {\n      int res;\n      res = myFunction(1, 2);\n      write(res);\n      writeln();\n    }\n    int myFunction(int arg1, int arg2) {\n      int local;\n      local = 1337;\n      return local + arg1 + arg2;\n    }\n  }\n#+END_SRC\n\n*** Assembly\n#+BEGIN_SRC asm\n#+END_SRC\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fabertschi%2Fbeancd-lang","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fabertschi%2Fbeancd-lang","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fabertschi%2Fbeancd-lang/lists"}