1 / 31

Languages and Compilers (SProg og Oversættere)

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.

jonesralph
Download Presentation

Languages and Compilers (SProg og Oversættere)

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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.

  2. 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

  3. 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 ;-)

  4. 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

  5. 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

  6. Program counter Data registers Arithmetic/Logic Unit Interpreter Basic Computer Architecture External files Main memory Cache memory CPU

  7. 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

  8. 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)

  9. 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

  10. 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

  11. 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.

  12. 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

  13. 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!

  14. Binding • Binding: an association between an attribute and its entity • Binding Time: when does it happen? • … and, when can it happen?

  15. 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

  16. 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

  17. 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

  18. 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

  19. 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}

  20. 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

  21. 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

  22. program main; var x : integer; procedure sub1; var x : integer; begin { sub1 } … x … end; { sub1 } begin { main } … x … end; { main } Example

  23. 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

  24. 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

  25. 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

  26. 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

  27. 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

  28. 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)

  29. Syntactic Elements • Definitions • Declarations • Expressions • Statements • Separate subprogram definitions (Module system) • Separate data definitions • Nested subprogram definitions • Separate interface definitions

  30. 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

  31. 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

More Related