310 likes | 326 Views
Dive into the world of programming language design, exploring concepts like programming models and language implementation. Learn about hardware and software computers, data operations, storage management, and more.
E N D
Languages and Compilers(SProg og Oversættere) Bent Thomsen Department of Computer Science Aalborg University With acknowledgement to Juan Carlos Guzmánand Elsa Gunter who’s slides this lecture is based on.
Some more Programming Language Design Issues • A Programming model (sometimes called the computer) is defined by the language semantics • More about this in the semantics course • Programming model given by the underlying system • Hardware platform and operating system • The mapping between these two programming models (or computers) that the language processing system must define can be influenced in both directions • E.g low level features in high level languages • Pointers, arrays, for-loops • Hardware support for fast procedure calls
Programming Language Implementation • Develop layers of machines, each more primitive than the previous • Translate between successive layers • End at basic layer • Ultimately hardware machine at bottom • To design programming languages and compilers, we thus need to understand a bit about computers ;-)
Why So Many Computers? • It is economically feasible to produce in hardware (or firmware) only relatively simple computers • More complex or abstract computers are built in software • There are exceptions • EDS machine to run prolog (or rather WAM) • Alice Machine to run Hope
Hardware computer: built out of wires, gates, circuit boards, etc. An elaboration of the Von Neumann Machine Software simulated computer: that implemented in software, which runs on top of another computer Data Primitive Operations Sequence Control Data Access Storage Management Operating Environment Machines Von Neumann Machine
Program counter Data registers Arithmetic/Logic Unit Interpreter Basic Computer Architecture External files Main memory Cache memory CPU
Memory and data • Memory • Registers • PC, data or address • Main memory (fixed length words 32 or 64 bits) • Cache • External • Disc, CD-ROM, memory stick, tape drives • Order of magnitude in access speed • Nanoseconds vs milliseconds • Built-in data types • integers, floating point, fixed length strings, fixed length bit strings
Hardware computer • Operations • Arithmetic on primitive data • Tests (test for zero, positive or negative) • Primitive access and modification • Jumps (unconditional, conditional, return) • Sequence control • Next instruction in PC (location counter) • Some instructions modify PC • Data access • Reading and writing • Words from main memory, Blocks from external storage • Storage management • Wait for data or multi-programming • Paging • Cache (32K usually gives 95% hit rate)
Micro Program interpretation and execution Fetch next instruction Decode instruction Operation and operands Fetch designated operands Branch to designated operation Execute Primitive Operation Execute Primitive Operation Execute Primitive Operation Execute Primitive Operation Execute halt
Virtual Computers • How can we execute programs written in the high-level computer, given that all we have is the low-level computer? • Compilation • Translate instructions to the high-level computer to those of the low-level • Simulation (interpretation) • create a virtual machine • Sometimes the simulation is done by hardware • This is called firmware
A Six-Level Computer Applications Level 5 Application Level Compilers, Editors, Navigators Software Level 4 Assembly Language Level Assembler, Linker, Loader Level 3 Operating System Machine Level Operating System Level 2 Instruction Set Architecture Level Hardware Microprogram or hardware Level 1 Microarchitecture Level Hardware Level 0 Digital Logic Level from Andrew S. Tanenbaum, Structured Computer Organization, 4th Edition, Prentice Hall, 1999.
A Web Application Input Output Web Application Computer (HTML web pages) Web Virtual Computer (Navigator implemented in C or Java) C Virtual Computer (standard C libraries) Operating System Virtual Computer (implemented in “machine Instructions”) Firmware Virtual Computer (implemented in microcode) Actual Hardware Computer
Back to programming language design • Now we know a bit about hardware, firmware and virtual machines • What is the impact on programming language design? • We need to make decisions about primitive data, data objects and program structures (syntax design) • But first we have to talk about binding time!
Binding • Binding: an association between an attribute and its entity • Binding Time: when does it happen? • … and, when can it happen?
Binding of Data Objects and Variables • Attributes of data objects and variables have different binding times • If a binding is made before run time and remains fixed through execution, it is called static • If the binding first occurs or can change during execution, it is called dynamic
Static Language definition time Language implementation time Program writing time Compile time Link time Load time Dynamic Run time At the start of execution (program) On entry to a subprogram or block When the expression is evaluated When the data is accessed Binding Time
X = X + 10 • Set of types for variable X • Type of variable X • Set of possible values for variable X • Value of variable X • Scope of X • lexical or dynamic scope • Representation of constant 10 • Value (10) • Value representation (10102) • big-endian vs. little-endian • Type (int) • Storage (4 bytes) • stack or global allocation • Properties of the operator + • Overloaded or not
Little- vs. Big-Endians • Big-endian • A computer architecture in which, within a given multi-byte numeric representation, the most significant byte has the lowest address (the word is stored `big-end-first'). • Motorola and Sun processors • Little-endian • a computer architecture in which, within a given 16- or 32-bit word, bytes at lower addresses have lower significance (the word is stored `little-end-first'). • Intel processors from The Jargon Dictionary - http://info.astrian.net/jargon
Under static, sometimes called lexical, scope, sub1 will always reference the x defined in big Under dynamic scope, the x it references depends on the dynamic state of execution procedure big; var x: integer; procedure sub1; begin {sub1} ... x ... end; {sub1} procedure sub2; var x: integer; begin {sub2} ... sub1; ... end; {sub2} Static vs. Dynamic Scope begin {big} ... sub1; sub2; ... end; {big}
Scope of Variable • Range of program that can reference that variable (ie access the corresponding data object by the variable’s name) • Variable is local to program or block if it is declared there • Variable is nonlocal to program unit if it is visible there but not declared there
Static Scoping • Scope computed at compile time, based on program text • To determine the name of a used variable we must find statement declaring variable • Subprograms and blocks generate hierarchy of scopes • Subprogram or block that declares current subprogram or contains current block is its static parent • General procedure to find declaration: • First see if variable is local; if yes, done • If non-local to current subprogram or block recursively search static parent until declaration is found • If no declaration is found this way, undeclared variable error detected
program main; var x : integer; procedure sub1; var x : integer; begin { sub1 } … x … end; { sub1 } begin { main } … x … end; { main } Example
Dynamic Scope • Now generally thought to have been a mistake • Main example of use: original versions of LISP • Common LISP uses static scope • Perl allows variables to be decalred to have dynamic scope • Determined by the calling sequence of program units, not static layout • Name bound to corresponding variable most recently declared among still active subprograms and blocks
program main; var x : integer; procedure sub1; begin { sub1 } … x … end; { sub1 } procedure sub2; var x : integer; begin { sub2 } … call sub1 … end; { sub2 } … call sub2… end; { main } Example
Binding Times summary • Language definition time: • language syntax and semantics, scope discipline • Language implementation time: • interpreter versus compiler, • aspects left flexible in definition, • set of available libraries • Compile time: • some initial data layout, internal data structures • Link time (load time): • binding of values to identifiers across program modules • Run time (execution time): • actual values assigned to non-constant identifiers The Programming language designer and compiler implementer have to make decisions about binding times
Readability syntactic differences reflect semantic differences verbose, redundant Writeability concise Ease of verifiability simple semantics Ease of translation simple language simple semantics Lack of ambiguity dangling else Fortran’s A(I,J) Syntax Design Criteria
Character set Identifiers Operators Keywords Noise words Elementary data numbers integers floating point strings symbols Delimiters Comments Blank space Layout Free- and fixed-field formats Lexical Elements
Some nitty gritty decisions • Primitive data • Integers, floating points, bit strings • Machine dependent or independent (standards like IEEE) • Boxed or unboxed • Character set • ASCII, EBCDIC, UNICODE • Identifiers • Length, special start symbol (#,$...), type encode in start letter • Operator symbols • Infix, prefix, postfix, precedence • Comments • REM, /* …*/, //, !, … • Blanks • Delimiters and brackets • Reserved words or Keywords (noise words)
Syntactic Elements • Definitions • Declarations • Expressions • Statements • Separate subprogram definitions (Module system) • Separate data definitions • Nested subprogram definitions • Separate interface definitions
Subprograms shallow definitions C nested definitions Pascal Data (OO) shallow definitions C++, Java, Smalltalk Separate Interface C, Fortran ML, Ada Mixed data and programs C Basic Others Cobol Data description separated from executable statements Data and procedure division Overall Program Structure
Some advice from an expert • Programming languages are for people • Design for yourself and your friends • Give the programmer as much control as possible • Aim for brevity • Admit what hacking is