Automating Fortran - C Interoperability Prepared by: Sisi Liu (Garnet) Mentor: Dan Nagle Co-Mentor: Davide del Vento 1.

Slides:



Advertisements
Similar presentations
Vilmos Zsombori , Shanghai
Advertisements

CPSC 388 – Compiler Design and Construction
Chapter3: Language Translation issues
1.3 Executing Programs. How is Computer Code Transformed into an Executable? Interpreters Compilers Hybrid systems.
Compilation (Chapter 3) 1 Course Overview PART I: overview material 1Introduction 2Language processors (tombstone diagrams, bootstrapping) 3Architecture.
COP4020 Programming Languages
Semantic Analysis (Generating An AST) CS 471 September 26, 2007.
Chapter 1 Introduction Dr. Frank Lee. 1.1 Why Study Compiler? To write more efficient code in a high-level language To provide solid foundation in parsing.
CSC 338: Compiler design and implementation
CST320 - Lec 11 Why study compilers? n n Ties lots of things you know together: –Theory (finite automata, grammars) –Data structures –Modularization –Utilization.
1 COMP 3438 – Part II-Lecture 1: Overview of Compiler Design Dr. Zili Shao Department of Computing The Hong Kong Polytechnic Univ.
Unit-1 Introduction Prepared by: Prof. Harish I Rathod
Algorithms  Problem: Write pseudocode for a program that keeps asking the user to input integers until the user enters zero, and then determines and outputs.
Overview of Previous Lesson(s) Over View  A program must be translated into a form in which it can be executed by a computer.  The software systems.
1 Compiler Design (40-414)  Main Text Book: Compilers: Principles, Techniques & Tools, 2 nd ed., Aho, Lam, Sethi, and Ullman, 2007  Evaluation:  Midterm.
CS536 Semantic Analysis Introduction with Emphasis on Name Analysis 1.
Introduction to Compiling
Chapter 1 Introduction Major Data Structures in Compiler
DOCUMENTATION SECTION GLOBAL DECLARATION SECTION
INTRODUCTION TO COMPILERS(cond….) Prepared By: Mayank Varshney(04CS3019)
What am I? while b != 0 if a > b a := a − b else b := b − a return a AST == Abstract Syntax Tree.
C H A P T E R T W O Linking Syntax And Semantics Programming Languages – Principles and Paradigms by Allen Tucker, Robert Noonan.
LECTURE 3 Compiler Phases. COMPILER PHASES Compilation of a program proceeds through a fixed series of phases.  Each phase uses an (intermediate) form.
©SoftMoore ConsultingSlide 1 Structure of Compilers.
MiniJava Compiler A multi-back-end JIT compiler of Java.
CS 404Ahmed Ezzat 1 CS 404 Introduction to Compiler Design Lecture 1 Ahmed Ezzat.
CS 404Ahmed Ezzat 1 CS 404 Introduction to Compiler Design Lecture Ahmed Ezzat.
Chapter 1 Introduction Samuel College of Computer Science & Technology Harbin Engineering University.
Open Source Compiler Construction (for the JVM)
Chapter 1. Introduction.
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Lecture 9 Symbol Table and Attributed Grammars
Advanced Computer Systems
Compiler Design (40-414) Main Text Book:
PRINCIPLES OF COMPILER DESIGN
Introduction to Compiler Construction
Lexical and Syntax Analysis
Introduction to Parsing (adapted from CS 164 at Berkeley)
Compiler Construction (CS-636)
SOFTWARE DESIGN AND ARCHITECTURE
Overview of Compilation The Compiler Front End
Overview of Compilation The Compiler Front End
Semantic Analysis with Emphasis on Name Analysis
Algorithms Problem: Write pseudocode for a program that keeps asking the user to input integers until the user enters zero, and then determines and outputs.
CSE 374 Programming Concepts & Tools
PROGRAMMING LANGUAGES
-by Nisarg Vasavada (Compiled*)
Introduction to C Topics Compilation Using the gcc Compiler
Chapter 1: Introduction to Compiling (Cont.)
CS 536 / Fall 2017 Introduction to programming languages and compilers
4 (c) parsing.
Emily Leland (Not Nick) Spring 2017
Basic Program Analysis: AST
Introduction CI612 Compiler Design CI612 Compiler Design.
Chapter 11 Introduction to Programming in C
CSE 3302 Programming Languages
CSE401 Introduction to Compiler Construction
COP4020 Programming Languages
Govt. Polytechnic,Dhangar
LL and Recursive-Descent Parsing Hal Perkins Autumn 2011
Adapted from slides by Nicholas Shahan and Dan Grossman
Adapted from slides by Nicholas Shahan, Dan Grossman, and Tam Dang
Nicholas Shahan Spring 2016
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
LL and Recursive-Descent Parsing Hal Perkins Autumn 2009
LL and Recursive-Descent Parsing Hal Perkins Winter 2008
Lec00-outline May 18, 2019 Compiler Design CS416 Compiler Design.
SPL – PS1 Introduction to C++.
Faculty of Computer Science and Information System
Chapter 11 Introduction to Programming in C
Presentation transcript:

Automating Fortran - C Interoperability Prepared by: Sisi Liu (Garnet) Mentor: Dan Nagle Co-Mentor: Davide del Vento 1

Background: Fortran-C Interoperability Scientific programs supported by the large systems at NCAR are often written in a combination of Fortran and C Fortran modules connect C library functions to the Fortran program with Fortran compiler’s support Currently, modules are written manually to express C interfaces. In other words, the translation from C to Fortran is done by hand. 2

Background: Fortran-C Interoperability 3

Introduction: Automating Fortran Project What is the object of this project? – Build a source to source translation tool – Translate C headers to Fortran modules to let Fortran programs use C library functions Why such tool is needed? – Automated module generation helps avoid redundant work and error-prone manual laboring – Easy for maintenance and update 4

Introduction: Automating Fortran Project 5

Approaches: Finding the Right Tool ANTLR – Lexer and parser tool – manually write the parser, last resort GCC – GENERIC – complicated and hard to modify – GIMPLE – unused variables are optimized (nearly the entire header is removed in the output) ROSE Compiler – Intermediate representation and support both C and Fortran – lack of public APIs Clang LLVM – Clang AST – modular design, abundant APIs 6

Abstract Syntax Tree (AST) A tree representati on of the abstract syntactic structure of code widely used in compilers as intermediate representation Language- independent A Simple Abstract Syntax Tree Example, tokens of the source code become tree nodes. 7

Steps to Automate Source-to-Source Translation 1.Lexical analysis and parse C to AST 2.Traversing through each node of AST recursively i.Identify the node: cast to specific declaration, type or expression ii.Tree structure. May expand to more nodes. (e.g. FunctionDecl expands to return type and multiple VarDecl) iii.Create a object for each node with all needed info for translation 3.Unparse the content of each node to Fortran code by calling the “formatASString()” method of this object to dump the Fortran code 8

Source-to-Source Translation Workflow 9

Implementation—Using Clang Tool to Traverse AST Clang AST Node Structure Overview – Declaration FunctionDecl—declares functions, might contain a function body VarDecl—declares values or types to variables RecordDecl—declares struct or union EnumDecl—declares enumerations TypeDefDecl—defines a type to variables – Statement (function body, unlikely to be seen in headers) – Type (inside declaration) Iso_c_binding—convert intrinsic C types to Fortran types Type defined identifiers Types can be more complicated 10

The source code can expand to very long abstract syntax tree! 11

Implementation—Declarations Function Declaration – Expand to return and argument type – Support intrinsic types: all literal type, c ptr and funptr, and type defined identifiers – Function body is preserved as comment – Unnamed arguments are automatically named as arg_1, arg_2… – Add import attribute to import derived types 12

Implementation—Declarations Record Declaration (struct) and Typedef Declaration – Translate as derived types in Fortran – Identifier is required in Fortran but not in C – Anonymous struct will be preserved as comment Enumeration Declaration – Directly translate to Fortran Enumeration 13

Implementation— Variable Declarations VarDecl without an init value – Support all intrinsic and declared types – Names that are valid in C (“_id”) but not in Fortran are commented out VarDecl with an init value – Support literal type, pointers, string and multidimensional array 14

Implementation—Macros are Different Macros are not part of AST but preprocessor Preprocessor functions only return the macro kind (object or function) and the source text Unlike dealing with object-oriented AST, values or types have to be manually parsed Limited types (only intrinsic types) are supported 15

Implementation—Macros Object Macro – Literal value declaration (e.g. #define INT_VARIABLE 128 )->variable declaration – Alias type declaration (e.g. #define long unsigned long int )->derived type – Identifier declaration (e.g. #define id1 id2 ) -> no type information, so commented out Function Macro (e.g. #define SQUARE(x) (x*x) ) – Lack of argument and return types (assume to be integer) ->function declaration 16

Example: PTHREAD_CANCELED is commented out due to the value’s type is non-trivial PTHREAD_MUTEX_DEFAUL T is commented out because there is no type information for this declaration 17

Overall Performance About 40% of the system headers in the /usr/include can be correctly compiled Another 40% headers contain unavoidable compilation errors (minor errors that need to be manually modified) – Same module (file name) and function name, not allowed in Fortran but allowed in C – Some symbols are valid in C but not in Fortran (e.g. ‘_id’ and ‘id-1’) – Type defined (tag named) struct with an identifier will be declared twice due to they are 2 separate nodes in AST 18

Future Work Potential improvements for the rest 20% headers – Non-intrinsic undeclared types cannot be recognized such as unrecognized_type(void (Tcl_Time *, ClientData) – No suitable auto translation for Union in Fortran – Remove the redundant struct that is defined twice – Out of source types that are defined other files can cause undeclared variables errors 19

Brief Demo 20 Shell script of using the tool to generate pthread module A mini Fortran hello world program that uses Fortran pthread module

Acknowledgement Many thanks to: Mentors: Dan Nagle and Davide Del Vento NCAR and SIParCS staff 21

References H2m tool repository: – Building instruction: – autoFortranTool/blob/master/Tool%20Building%20Instruct ion.pdf autoFortranTool/blob/master/Tool%20Building%20Instruct ion.pdf Figure reference: – Performing Source-to-Source Transformations with Clang by Olaf Krzikalla 22