The C Language the C Language C History BCPL C History C History
Total Page:16
File Type:pdf, Size:1020Kb
The C Language The C Language Currently, the most commonly-used language for embedded systems “High-level assembly” Prof. Stephen A. Edwards Very portable: compilers exist for virtually every processor Easy-to-understand compilation Produces efficient code Fairly concise Copyright © 2001 Stephen A. Edwards All rights reserved Copyright © 2001 Stephen A. Edwards All rights reserved C History BCPL Developed between 1969 and 1973 along with Unix Designed by Martin Richards (Cambridge) in 1967 Due mostly to Dennis Ritchie Typeless Designed for systems programming • Everything an n-bit integer (a machine word) • Pointers (addresses) and integers identical • Operating systems • Utility programs Memory is an undifferentiated array of words • Compilers Natural model for word-addressed machines • Filters Local variables depend on frame-pointer-relative Evolved from B, which evolved from BCPL addressing: dynamically-sized automatic objects not permitted Strings awkward • Routines expand and pack bytes to/from word arrays Copyright © 2001 Stephen A. Edwards All rights reserved Copyright © 2001 Stephen A. Edwards All rights reserved C History C History Original machine (DEC PDP-11) Many language features designed to reduce memory was very small • Forward declarations required for everything • 24K bytes of memory, 12K used • Designed to work in one pass: must know everything for operating system • No function nesting Written when computers were big, capital equipment PDP-11 was byte-addressed • Group would get one, develop new language, OS • Now standard • Meant BCPL’s word-based model was insufficient Copyright © 2001 Stephen A. Edwards All rights reserved Copyright © 2001 Stephen A. Edwards All rights reserved 1 Hello World in C Hello World in C Program mostly a collection of functions ¢¡¤£¦¥¨§¨© ¢ ¡¦¦ ¢¡¤£¦¥¨§¨© ¢ ¡¦¦ Preprocessor used to share information “main” function special: among source files the entry point ¤¡ ¤¨¡£¢ ¤¡ ¤¨¡£¢ - Clumsy “void” qualifier indicates function does not return ¡£¦ #$¦%& ¨§¤§¦')( §¨ +*-,.£¤/¦10 ¡£¦ #$¦%& ¨§¤§¦')( §¨ +*-,.£¤/¦10 " !¢" " !¢" + Cheaply implemented anything 2 2 + Very flexible I/O performed by a library function: not included in the language Copyright © 2001 Stephen A. Edwards All rights reserved Copyright © 2001 Stephen A. Edwards All rights reserved Euclid’s algorithm in C Euclid’s algorithm in C ¡3£4¤¥ 5¡£¦67'8¡¤£9£¦ ¡3£4¤¥ 5¡£¦67'8¡¤£9£¦ “New Style” function declaration lists number and type of Automatic variable ¡¤£¦ 0 ¡¤£¦ 0 " " arguments Storage allocated on (5¡¨§ :; 6>:£¦?* (5¡¨§ :; 6>:£¦?* <;@ "=< <;@ "=< Originally only listed stack when function £A0 £A0 < < return type. entered, released £ 0 £ 0 <:" <:" Generated code did when it returns. 2 2 not care how many All parameters, arguments were ¨¤© £;£A0 ¨¤© £;£A0 " " " " Excess automatic variables actually passed. arguments 2 2 accessed w.r.t. simply frame pointer. Arguments are call- n by-value ignored m Extra storage Frame ret. addr. needed while pointer Stack r evaluating large pointer expressions also placed on the stack Copyright © 2001 Stephen A. Edwards All rights reserved Copyright © 2001 Stephen A. Edwards All rights reserved Euclid’s algorithm in C Euclid’s algorithm in C ¡3£4¤¥ 5¡£¦67'8¡¤£9£¦ ¡3£4¤¥ 5¡£¦67'8¡¤£9£¦ Expression: C’s ¡¤£¦ 0 ¡¤£¦ 0 " " basic type of (5¡¨§ :; 6>:£¦?* (5¡¨§ :; 6>:£¦?* <;@ "=< <;@ "=< statement. £A0 £A0 < < Arithmetic and £ 0 £ 0 <:" <:" logical 2 2 Assignment (=) High-level control-flow ¨¤© £;£A0 ¨¤© £;£A0 " " " " returns a value, so Each function statement. Ultimately 2 2 can be used in returns a single becomes a conditional expressions value, usually an branch. integer. Returned through a specific Supports “structured % is remainder register by programming” != is not equal convention. Copyright © 2001 Stephen A. Edwards All rights reserved Copyright © 2001 Stephen A. Edwards All rights reserved 2 Euclid Compiled on PDP-11 Euclid Compiled on PDP-11 ¡£¢¥¤§¦¨¢ © ¡§ £ ¡£¢¥¤§¦¨¢ © ¡§ Very natural mapping from C into ! #" &%' )( * $ $ §§ §§ PDP-11 instructions. ©£¡§ ,+ ©£¡§ ,+ - - ¤ 1* .*! & .*! & Complex addressing modes make /¥0£ /0£123£465 /¥7£12 §4¥§ /¥0£ 89 89 ¤ ¥*>' ? &?3@ + ¤ ¥*>' ? + frame-pointer-relative accesses 0;:=< 4 7 0;:=< 7 int gcd(int m, int n) ¡ & & easy. 46¥§£4 ¥§ ¥§ { % ¥*>' & @C&, &&? % ¥*>' & < 71AB4 ¥0 < ¥0 int r; Another idiosyncrasy: registers ¤ ?' D¥? ¥*> E@ ¤ ?' D¥? ¥*> 0 < 76FB4 7 0 < 7 while ( (r = m % n) != 0) { were memory-mapped, so taking - - 8§H 8§H m = n; G G address of a variable in a register n = r; ¤ % ¥*>' ¥*> @ ¤ % ¥*>' ¥*> 0 < :=< 7 4 7 0 < :=< 7 is straightforward. } ¤ ? ¥*>, I% ¥*> @J ¤ ? ¥*>, I% ¥*> 0 < < 4 7 0 < < 7 return n; - - 8¥9 8¥9 ¦K ¦K } 8H 8H + ¤ % ¥*>' & + ¤ % ¥*>' & 7 0 < ¨L 4146¨4 7 0 < - - 8 8 ¦K ? ¦K ? - - 8 8 ?'+ § ¤M N ?'+ § 7¨2 4 3§212 §L 4 7¨2 4 Copyright © 2001 Stephen A. Edwards All rights reserved Copyright © 2001 Stephen A. Edwards All rights reserved Pieces of C C Types Types and Variables Basic types: char, int, float, and double • Definitions of data in memory Meant to match the processor’s native types Expressions • Natural translation into assembly • Arithmetic, logical, and assignment operators in an • Fundamentally nonportable infix notation Declaration syntax: string of specifiers followed by a Statements declarator • Sequences of conditional, iteration, and branching instructions Declarator’s notation matches that in an expression Functions Access a symbol using its declarator and get the basic type back • Groups of statements and variables invoked recursively Copyright © 2001 Stephen A. Edwards All rights reserved Copyright © 2001 Stephen A. Edwards All rights reserved C Type Examples C Typedef ¡3£;¡ 0 Integer Type declarations recursive, complicated. ¡3£PO!Q+'CRA0 j: pointer to integer, int k Name new types with typedef ©3£ ¦¡ 43£¦ ¥ O¨¥¤10 " ch: pointer to unsigned char #§¦¨¨;#TSU 0 @'V Array of 10 floats Instead of ¥3 £¦ MW¤!X¢ ¡£'8¥¤ O¨ 0 ¡3£:¨O #¨©£¥=] . ¡ " " " 2-arg function ¡3£YS§Z S¨[ SU 0 V @\V V Array of three arrays of five … use ¡3£PO #¤©¤£¥'U¢ #¨§ ¨¨¨+0 !^ ¨ ¨ ¨#;¡£¦ #©¤£¥!]¨5 3¡ ¨ 0 ! function returning int * ¡3£:¨O #¨©£¥=] . ¡ 10 #©¤£¦¥=] PO #¤©£¦¥!]+0 pointer to function returning int Copyright © 2001 Stephen A. Edwards All rights reserved Copyright © 2001 Stephen A. Edwards All rights reserved 3 C Structures Struct bit-fields A struct is an object with named fields: Way to aggressively pack data in memory ©¨¥ ©¨¥ " " ¡ ¥¤¦ O£¦ ¨ 0 ©¤£¨¡ 4£¦ ¨ ;¡¤£ © ¤£ [50 " ¡¤£¦ W+'^70 ©¤£¨¡ 4£¦ ¨ ;¡¤£ ¨¡ ]¥£ U 0 ¡¤£¦ ')( 0 ©¤£¨¡ 4£¦ ¨ ;¡¤£;©¤ §¦¢ MW £¦¤§¨¦5¥ §¤¥,R©£ UA0 " 2¢¡ 2 =W0 #¨§ ¨43 0 Compiler will pack these fields into words Accessed using “dot” notation: Very implementation dependent: no guarantees of ¡ =W+W [ 0 < ordering, packing, etc. ¡ =W+ ^ ]+0 < Usually less efficient • Reading a field requires masking and shifting Copyright © 2001 Stephen A. Edwards All rights reserved Copyright © 2001 Stephen A. Edwards All rights reserved C Unions Alignment of data in structs Can store objects of different types at different times Most processors require n-byte objects to be in memory at address n*k ©3£¦¡¦¢£ Side effect of wide memory busses ¡¤£ ¡ ¤§ 0 #¨§¦¤¨# ¤§10 E.g., a 32-bit memory bus ¥¤ O¤ ¤§10 " Read from address 3 requires two accesses, shifting 2 0 1 Useful for arrays of dissimilar objects 4 3 2 Potentially very dangerous Good example of C’s philosophy 4 3 2 1 • Provide powerful mechanisms that can be abused Copyright © 2001 Stephen A. Edwards All rights reserved Copyright © 2001 Stephen A. Edwards All rights reserved Linker-visible. Alignment of data in structs C Storage Classes Allocated at fixed location ¡ ¢¡¤£¦¥¨§¨© ¢ § ¡ Compilers add “padding” to structs to ensure proper Visible within file. alignment, especially for arrays Allocated at fixed ¡ ¤§§¦5¤ ¤¡ ¥+0 ¡3£4¤§ location. ¦5 ¨¨¡¤¥10 Pad to ensure alignment of largest object (with ¤¡¨¥¡£¦#¨¡¤§ biggest requirement) a ¤¡ ;# ¡£;¨© ¦ ¤ ! " ©¨¥ " b b b b ¥¤¦ +0 " c ¦5¦ ¤¨¡ ¥ 0 ¨¨¤¡ ¥;¡£;#¨©£¦¥ Visible within func. ¡ ¡¤£¦ 0 ¦¢¡1'8¤©¨ ¦¢TS§U 0 Pad ¡¤£¦;¨© Allocated at fixed @\V ¥¤¦ ¥ 0 " location. ¡ ¡ © § O ¨©¨ ¦¢ ¨§ §¦¥5¦¡ ¨# © § ¤=O [ 10 < 2 a 2 b b b b c Moral: rearrange to save memory Copyright © 2001 Stephen A. Edwards All rights reserved Copyright © 2001 Stephen A. Edwards All rights reserved 4 C Storage Classes malloc() and free() ¡ ¢¡¤£¦¥¨§¨© ¢ § ¡ Library routines for managing the heap ¡ ¡3£4¤§ ¤§§¦5¤ ¤¡ ¥+0 ¦5 ¨¨¡¤¥10 ¤¡¨¥¡£¦#¨¡¤§ int *a; a = (int *) malloc(sizeof(int) * k); ¡£;¨© ¦ ¤ ¤¡ ;# Space allocated on ! " a[5] = 3; stack by caller. free(a); ¦5¦ ¤¨¡ ¥ 0 ¨¨¤¡ ¥;¡£;#¨©£¦¥ Space allocated on ¡¤£¦;¨© ¦¢¡1'8¤©¨ ¦¢TS§U 0 @\V stack by function. ¡ ¡ § O ¨©¨ ¦¢ ¨§ §¦¥5¦¡ ¨# © § ¤=O [ 10 © Allocate and free arbitrary-sized chunks of memory < 2 in any order Space allocated on heap by library routine. Copyright © 2001 Stephen A. Edwards All rights reserved Copyright © 2001 Stephen A. Edwards All rights reserved malloc() and free() malloc() and free() More flexible than automatic variables (stacked) Memory usage errors so pervasive, entire successful company (Pure Software) founded to sell tool to track More costly in time and space them down • malloc() and free() use complicated non-constant-time algorithms Purify tool inserts code that verifies each memory • Each block generally consumes two additional words access of memory Pointer to next empty block Reports accesses of uninitialized memory, Size of this block unallocated memory, etc. Common source of errors Publicly-available