C2 language. Bas van den Berg. Bas van den Berg () C2 language / 37

Similar documents
1 Abstract Data Types Information Hiding

How To Port A Program To Dynamic C (C) (C-Based) (Program) (For A Non Portable Program) (Un Portable) (Permanent) (Non Portable) C-Based (Programs) (Powerpoint)

CORBA Programming with TAOX11. The C++11 CORBA Implementation

An Incomplete C++ Primer. University of Wyoming MA 5310

Object Oriented Software Design II

Storage Classes CS 110B - Rule Storage Classes Page 18-1 \handouts\storclas

Embedded Software Development with MPS

Semantic Analysis: Types and Type Checking

Chapter 13 Storage classes

How To Use The C Preprocessor

KITES TECHNOLOGY COURSE MODULE (C, C++, DS)

3.5. cmsg Developer s Guide. Data Acquisition Group JEFFERSON LAB. Version

The GenomeTools Developer s Guide

C++ Programming Language

Object Oriented Software Design II

Basic Java Constructs and Data Types Nuts and Bolts. Looking into Specific Differences and Enhancements in Java compared to C

Scoping (Readings 7.1,7.4,7.6) Parameter passing methods (7.5) Building symbol tables (7.6)

ECE 341 Coding Standard

Sources: On the Web: Slides will be available on:

Building Software Systems. Multi-module C Programs. Example Multi-module C Program. Example Multi-module C Program

Dice. David Watkins Emily Chen Khaled Atef Phillip Schiffrin. djw2146 ec2805 kaa2168 pjs2186. Manager System Architect Testing Language Guru

Informatica e Sistemi in Tempo Reale

Applying Clang Static Analyzer to Linux Kernel

Install Java Development Kit (JDK) 1.8

SMTP-32 Library. Simple Mail Transfer Protocol Dynamic Link Library for Microsoft Windows. Version 5.2

Embedded Programming in C/C++: Lesson-1: Programming Elements and Programming in C

Comp151. Definitions & Declarations

Environnements et Outils de Développement Cours 6 Building with make

An Overview of Java. overview-1

Phys4051: C Lecture 2 & 3. Comment Statements. C Data Types. Functions (Review) Comment Statements Variables & Operators Branching Instructions

Technical paper review. Program visualization and explanation for novice C programmers by Matthew Heinsen Egan and Chris McDonald.

Semantic Checker (Part I)

Code and Usage Description. A.Konstantinov

Parsing Technology and its role in Legacy Modernization. A Metaware White Paper

MPLAB TM C30 Managed PSV Pointers. Beta support included with MPLAB C30 V3.00

Object Oriented Software Design II

SQLITE C/C++ TUTORIAL

8.5. <summary> Cppcheck addons Using Cppcheck addons Where to find some Cppcheck addons

C Programming Review & Productivity Tools

8 Tutorial: Using ASN.1

Freescale Semiconductor, I

Static vs. Dynamic. Lecture 10: Static Semantics Overview 1. Typical Semantic Errors: Java, C++ Typical Tasks of the Semantic Analyzer

Network Programming. Writing network and internet applications.

MPLAB Harmony System Service Libraries Help

CSC230 Getting Starting in C. Tyler Bletsch

MatrixSSL Porting Guide

Static Code Analysis Procedures in the Development Cycle

Course Name: ADVANCE COURSE IN SOFTWARE DEVELOPMENT (Specialization:.Net Technologies)

Virtuozzo Virtualization SDK

User-level processes (clients) request services from the kernel (server) via special protected procedure calls

CS 241 Data Organization Coding Standards

Porting Lustre to Operating Systems other than Linux. Ken Hornstein US Naval Research Laboratory April 16, 2010

PetaLinux SDK User Guide. Application Development Guide

CS Software Engineering for Scientific Computing. Lecture 25:Mixed Language Programming.

Lab 5: BitTorrent Client Implementation

How To Write A Program In Anieme Frontend (For A Non-Programmable Language)

Pattern Insight Clone Detection

Generating Serialisation Code with Clang

Lecture 9. Semantic Analysis Scoping and Symbol Table

Fast Arithmetic Coding (FastAC) Implementations

The C Programming Language course syllabus associate level

Java from a C perspective. Plan

Lecture 22: C Programming 4 Embedded Systems

Netscape Internet Service Broker for C++ Programmer's Guide. Contents

FreeForm Designer. Phone: Fax: POB 8792, Natanya, Israel Document2

Using the Tcl VFS for Encryption

SharePoint 2010 Farm Restore

iphone Objective-C Exercises

/* File: blkcopy.c. size_t n

El Dorado Union High School District Educational Services

Compiler I: Syntax Analysis Human Thought

Binary compatibility for library developers. Thiago Macieira, Qt Core Maintainer LinuxCon North America, New Orleans, Sept. 2013

Introduction to Socket programming using C

C HAPTER E IGHTEEN T HE PGP, MAIL, AND CGI LIBRARIES. PGP Interface Library

Sample Applications User Guide. Release 2.0.0

Programming Guide for Moxa Device Manager

This documentation is made available before final release and is subject to change without notice and comes with no warranty express or implied.

XML Processing and Web Services. Chapter 17

Tutorial on C Language Programming

Chao Chen 1 Michael Lang 2 Yong Chen 1. IEEE BigData, Department of Computer Science Texas Tech University

Visual Basic Programming. An Introduction

Automated Faultinjection Series - Risk Management and Implementation

Server-Side Web Development JSP. Today. Web Servers. Static HTML Directives. Actions Comments Tag Libraries Implicit Objects. Apache.

03 - Lexical Analysis

Using EDA Databases: Milkyway & OpenAccess

How To Understand How A Process Works In Unix (Shell) (Shell Shell) (Program) (Unix) (For A Non-Program) And (Shell).Orgode) (Powerpoint) (Permanent) (Processes

<Insert Picture Here> What's New in NetBeans IDE 7.2

Xcode Project Management Guide. (Legacy)

Coding Rules. Encoding the type of a function into the name (so-called Hungarian notation) is forbidden - it only confuses the programmer.

Common Errors in C/C++ Code and Static Analysis

Component visualization methods for large legacy software in C/C++

Generating Visual Studio Project Files

Error Log Analysis for Improving Educational Materials in C Programming Language Courses

Java 7 Recipes. Freddy Guime. vk» (,\['«** g!p#« Carl Dea. Josh Juneau. John O'Conner

Compiler Construction

Overview. Lecture 1: an introduction to CUDA. Hardware view. Hardware view. hardware view software view CUDA programming

Origin Tracking in Attribute Grammars

Firewall Builder Architecture Overview

Introduction. dnotify

Crash Course in Java

Transcription:

C2 language Bas van den Berg 2014 Bas van den Berg () C2 language 2014 1 / 37

Table of contents 1 Intro 2 Design 3 Changes 4 Concept: types 5 Concept: multi-pass parsing 6 Concept: modules 7 Concept: build system 8 The C2 Project Bas van den Berg () C2 language 2014 2 / 37

Intro C Intro The C programming language has been around for a long time and is still used a lot nowadays. The core of the language is very solid, but other aspects are showing their age. C2 attempts to modernize these parts, while keeping the feel of C. It should be seen as an evolution of C. Bas van den Berg () C2 language 2014 3 / 37

C2 Design goals Design Higher development speed Same/better speed of execution Better compilation times Integrated build system Stricter syntax (easier for tooling) Great tooling (formatting tool, graphical refactoring tool) C2 programs can use C libraries (and vice-versa) Should be easy to learn for C programmers (evolution) Should support avoiding common mistakes Bas van den Berg () C2 language 2014 4 / 37

C2 Non-goals Design higher-level features (garbage collection, classes, etc) completely new language Bas van den Berg () C2 language 2014 5 / 37

C improvement points Design Lots of typing (header/forward declarations) Build system separate from language Variable syntax complex char *(*(**foo [][8])())[]; Bas van den Berg () C2 language 2014 6 / 37

From C to C2 Changes No header files No forward declarations No includes necessary Integrated compiler option syntax Integrated Build system Compilation per target, not file Simplified type syntax Stricter error checking (uninitialized var usage is error) More built-in types (uint8,uint16,uint32,int8,int16,int32,...) Some syntax cleanup... Bas van den Berg () C2 language 2014 7 / 37

Keyword changes Changes removed keywords: extern static typedef long short signed unsigned new keywords: module import as public local type func nil elemsof int8 int16 int32 int64 uint8 uint16 uint32 uint64 float32 float64 Bas van den Berg () C2 language 2014 8 / 37

Hello World! Changes hello world.c2 module hello_world; import stdio as io; func int main(int argc, char*[] argv) { io.printf("hello World!\n"); return 0; Spot the five differences... Bas van den Berg () C2 language 2014 9 / 37

Concept: types Example - Base Types types.c2 module types; public type Number int; type PNum int**; type IntArr int[20]; public type String const uint8*; type DoubleBufPtr DoubleBuf*; type DoubleBuf Buffer*[2]; All typedefs are uniform.. Bas van den Berg () C2 language 2014 10 / 37

Concept: types Example - Function Types function types.c2 module types; type CallBack func int(int a, utils.point* p); type CBFunc func void (MyType* mt,...); CBFunc[10] callbacks; Note: declaring an array/pointer to function types requires to steps. Bas van den Berg () C2 language 2014 11 / 37

Concept: types Example - Struct Types struct types.c2 module types; type ChessBoard struct { int[8][8] board; type Example struct { int n; union { int b; Point c; choice; volatile uint32 count; Bas van den Berg () C2 language 2014 12 / 37

Concept: types Feature - multi-part array initialization type examples.c2 module types; type Element struct { const char[16] name; int value; const Element[] elements; elements += { "test1", 10... elements += { "test2", 20... elements += { "test3", 30 This is possible because recipe file and multi-pass parsing. Bas van den Berg () C2 language 2014 13 / 37

Concept: multi-pass parsing multi-pass parsing example.c2 module example; Number hundred = 100; func Number add(number a, Number b) { return a + b; type Number int; Ordering in a file is not relevant for parsing. Variables, functions and types can be specified in any order. Bas van den Berg () C2 language 2014 14 / 37

Concept: modules Package scopes cause less prefixes in names graphics.c2 module graphics; public type Buffer {... public func void init() {... graphics.h (ANSI-C) #ifndef GRAPHICS_H #define GRAPHICS_H typedef struct {... Graphics_Buffer; void graphics_init(); public func \ void create(buffer* buf) {... void graphics_create( \ Graphics_Buffer* buf); #endif Bas van den Berg () C2 language 2014 15 / 37

Concept: modules Symbol accessibility application.c2 module gui; import utils local; Engine engine; // ok Engine_priv priv; // not ok my utils.c2 module utils; public type Engine struct {... type Engine_priv struct {... Only public symbols can be used outside the module. Non-public symbols can be used from any file within the same module. Non-public roughly translates to the C keyword static. Bas van den Berg () C2 language 2014 16 / 37

Opaque pointers Concept: modules application.c2 module application; import foolib; foolib.foo* foo; func void test() { foolib.init(foo); foolib.c2 module foolib; // non-public type Foo struct {... public func void init(foo* f) {... Use of pointers to non-public Types is allowed (but no de-referencing) Bas van den Berg () C2 language 2014 17 / 37

Multi-file module Concept: modules file1.c2 module utils; Type Number int; func void test() { tryme(); file2.c2 module utils; Number MAX = 20; Number[10] numbers; func void tryme() {... No need to import your own module. Treat as if all code is in the same file. Bas van den Berg () C2 language 2014 18 / 37

Concept: modules Multi-file module usage gui.c2 module gui; import utils local; utils buf.c2 module utils; public type Buffer int[10]; utils.buffer buf; func void run() { utils.log("ok"); log("also ok"); utils log.c2 module utils; public func void log(int8* msg) {... Bas van den Berg () C2 language 2014 19 / 37

Naming conflicts Concept: modules gui.c2 module gui; import graphics local; import file local; Buffer buffer; // not ok graphics.buffer buf1; // ok file.buffer buf2; // ok file.c2 module file; public type Buffer {... graphics.c2 module graphics; public type Buffer {... Use statements are have a file scope, not module scope. Bas van den Berg () C2 language 2014 20 / 37

Use statement Concept: modules file1.c2 module a_long_module_name; Type Number int; file2.c2 module foo; import a_long_module_name as other; other.number number; Syntax is import long as short to avoid too much typing, while making it clear where a symbol comes from. Bas van den Berg () C2 language 2014 21 / 37

Concept: modules Use statement - example file1.c2 module server; import network as net; import filesystem as fs; func fs.file* getfile(net.url url) { net.socket sock = net.open(server); fs.file* file = net.get(sock, url); net.close(sock); return file; = Both modules have been aliased. Bas van den Berg () C2 language 2014 22 / 37

Concept: modules Use statement - example 2 file1.c2 module server; import network as net local; import filesystem as fs local; func File* getfile(url url) { Socket sock = net.open(server); File* file = get(sock, url); net.close(sock); return file; = Both modules have been aliased and imported locally. Bas van den Berg () C2 language 2014 23 / 37

Concept: modules Use statement - example 3 file1.c2 module server; import network as net local; import filesystem as fs; func fs.file* getfile(url url) { Socket sock = open(server); fs.file* file = get(sock, url); close(sock); return file; = Only often used modules are imported locally. Bas van den Berg () C2 language 2014 24 / 37

the c2 module Concept: modules example.c2 module example; import c2; uint64 buildtime = c2.buildtime; const char* version = c2.version; const char*[] options = c2.options; A special module called c2 can be used to get compile-information, build-time, (git/svn) version, build-flags etc. So no need to script some of your own. Bas van den Berg () C2 language 2014 25 / 37

building Concept: build system To build C2 projects, simply use $ c2c This searches the current and parent directories for the recipe file (recipe.txt). This means c2c can be called from any subdir in the project, which is handy when working in a subdir: drivers/networking/ethernet$ c2c Bas van den Berg () C2 language 2014 26 / 37

recipe file Concept: build system Every C2 project has a recipe file in the root directory of the project. This file contains a list of all target that need to be build. For each target the recipe describes: name type - executable/library files - all required c2 files. This allows C2 to do better optimizations and error checking configuration - all defines used exports - which modules will be exported (visible as ELF object in the resulting file Bas van den Berg () C2 language 2014 27 / 37

Concept: build system example recipe file recipe.txt executable one example1/gui.c2 example1/utils.c2 end library mylib config NO_DEBUG WITH_FEATURE1 FEATURE2 export mylib example2/mylib1.c2 example2/mylib2.c2 end Bas van den Berg () C2 language 2014 28 / 37

outputs Concept: build system The results of building are stored in the output directory. So make clean is simply removing of this directory. Bas van den Berg () C2 language 2014 29 / 37

Build process Concept: build system Because of the language design, compiling C2 code requires a different process then compiling C. The basic process is as follows: parse all c2 files into ASTs check all ASTs generate IR code per module generate object code per module link all objects So in effect, all files are parsed simultaneously. Bas van den Berg () C2 language 2014 30 / 37

Concept: build system Build process difference C: a new compiler is started for each.c file C2 finds a compile error in file x much faster C2 generates code per module, not file The generation(+ optimization) step takes much longer then the parse/analyse step, so the yellow blocks are really much bigger Bas van den Berg () C2 language 2014 31 / 37

Concept: build system Build process per file first parse the file to the AST extract the symbol table and add to the global table generate IR code from the AST and Global Symbol table Bas van den Berg () C2 language 2014 32 / 37

Language Scope The C2 Project The scope of the C2 language is wider than the C language. For example, there is no syntax format for specifying attributes in the C language. In C2, the syntax is specified, there are common attributes and compiler-builders can add custom attributes without disturbing others. = widening the language scope allows for huge improvements and ease of use. Bas van den Berg () C2 language 2014 33 / 37

Tooling The C2 Project The language makes several interesting tooling options possible. Some of these options are grouped in c2reto, the C2 Refactor Tool: Visualizing dependencies between functions/vars/types/files/modules Drag N Drop reordering of declarations in files Drag N Drop moving of declarations between files renaming, style formatting, etc Bas van den Berg () C2 language 2014 34 / 37

Implementation The C2 Project The C2 compiler is currently built on top of llvm and uses the clang Lexer/Pre-processor. The parser and semantic analyser are custom. Code is translated to an AST (Abstract Syntax Tree) that s similar to clang s (but much simpler). After generating and checking the AST s, LLVM s IR code is generated from the AST. Bas van den Berg () C2 language 2014 35 / 37

Links The C2 Project http://www.c2lang.org http://github.com/c2lang/c2compiler Bas van den Berg () C2 language 2014 36 / 37

title The C2 Project Bas van den Berg () C2 language 2014 37 / 37