how can I view the c code generated from my pypy program - disassembly

Many language environments allow one to "disassemble" a provided function. Since Pypy compiles to C-code (if I understand things correctly). then it seems natural to be able to see a C-code dump from an expression, or a whole python file.
Can I do this? how?

PyPy does not compile python bytecode to C code. The tracing JIT replaces a frequently used piece of code with generated machine code (assembly). You can turn on logging to save the JIT traces to a file and view them with vmprof or jitviewer.

Related

How to create real compiler with Flex/Bison?

I'm learning Flex / Bison right now, thinking I can do a compiler, but the more I inquire the more I get the impression that they are only Syntactic Analyzers, and do not allow to generate new files executable universal windows from our programming language. I explain, when the file generated by Bison is executed, it is only interpreted our code language in C.
Is it possible to create a compiler that generates executable files from any windows that do not have my compiler?
Yes, it's possible (and perfectly common) to write compilers using flex+bison, but these tools only help you do the lexical and syntactic analysis. You'll have to do the rest yourself or using additional tools like LLVM.
For example to create a simple single-pass compiler you could simply write assembly instructions into a file from within your bison-actions. Then you could run that file through an assembler and linker at the end and get an executable.
Or for a more complicated compiler you might create an abstract syntax tree inside your bison actions and then walk that tree in later phases to perform transformations and analyses on it until you finally generate assembly.
Either way bison only helps you with the parsing, you'll need to perform the other steps yourself.

Where in the GCC source code does it compile to the different assembly languages?

Where is the code in the GCC source code that actually constructs the assembly for the different architectures?
Wondering how many different assembly languages it compiles to, and how it actually does this (by taking a look at the source code).
Is it in the gcc repo somewhere, or in another repo? I have started to dig around but haven't found anything.
https://github.com/gcc-mirror/gcc
For example, here is some of the assembly generating code in V8:
https://github.com/v8/v8-git-mirror/tree/master/src/x64
Is there anything equivalent for GCC?
I am wondering because it's a mystery how GCC does this, and it would be a great way to learn how compilers are actually implemented down to the assembly level.
The .md (machine description) files of GCC source contain stuff to generate assembly. GCC contains several specialized C/C++ code generators (and some of them translates the .md files into code emitting assembly).
GCC is a very complex program. The documentation of GCC MELT (an obsolete project) contains several interesting links and slides, notably refering to the Indian GCC Resource Center
Most of the optimizations in GCC happens in the middle-end (which is mostly independent of source language or target system), notably with many passes working on the Gimple representations.
The GCC repo is an SVN repository.
See also this answer, notably the pictures inside it.
The actual source code for GCC is most accessible from here:
https://gcc.gnu.org/svn.html
The software is accessible via SVN (subversion), a source code control system. This would be installed on many versions of Linux/UNIX, but if not on your platform, you can install the svn kit and then fetch the source using the following command:
svn checkout svn://gcc.gnu.org/svn/gcc/trunk SomeLocalDir
GCC is complex and would take significant experience to understand the nature of how the application actually compiles to different architectures.
In a nutshell, GCC has three major components - front-end, middle and back-end processing. The front-end processor has the component of the language parsing to understand the syntax of languages (like C, C++, Objective-C, etc). The front-end deconstructs the code to a portable construct which is then passed to the back-end for compilation to the target environment.
The middle part performs code analysis and optimisation, attempting to prioritise the code to generate the best possible output at the end of the full process. Technically, optimisation can occur at any part of the process as patterns are discovered during analysis.
The back-end processor compiles the code to a tree-style output format (not actually final executable code). Based on what the expected output is designed to be, the "pseudo-code" is optimised for using registers, bit-sizes, endian-ness, and so on. The final code is then generated during the assembly phase, which converts the back-end code into machine executable instructions.
It's important to note that the compiler has many options to deal with output formats so you can create output to many classes of architecture, usually out of the box. For cross-compiling and target compiler options, try checking out this link:
https://gcc.gnu.org/install/configure.html

How can I compile ANSI C99-based MEX code delivered with Linux makefiles under Win64 MATLAB?

It seems I've got a real problem here due to my lack of any knowledge about Linux systems:
I have downloaded some open source code, which
is written in C
uses complex.h, so I assume it is ANSI C99
comes with makefiles designed for compilation under Linux systems
provides interfaces to IDL, MATLAB, Python etc.
I am indeed familiar about compiling C/MEX files under Windows-based MATLAB environments, but in this case I don't even know where to start. The project is distributed in several folders and consists of dozens of source and header files. And, to begin with, the Visual Studio 2010 compiler I've used to compile MEX files until now does not comply with the C99 standard, i.e. it does not recognize the complex.h header.
Any help towards getting this project compiled would be highly appreciated. In particular, I have the following questions:
1) Is there any possibility to automatically extract compilation information from the MEX files and transfer it to Windows reality?
2) Is there any free compiler being able to compile C99 stuff, which is also easy to embed in MATLAB?
I have done this (moved in-house legacy code inc. mex files to Win64). I can't recommend the experience.
You will have to recompile, no way around it.
Supported compilers for mex depend on your MATLAB version
This File Exchange entry for using Pelles C may be a starting point (if it works with your version of MATLAB).
I am guessing that there is a main makefile which then works through the makefiles in the subdirectories - have a read through the instructions for compiling under Linux, it will give you some idea of what's going on and may also discuss what to do if you want to change compiler. Once you've found a compatible compiler, the next stage is to understand what the makefiles are doing and edit them accordingly (change paths, compiler, compiler flags, etc.)
Then, from memory (it was a while ago), you get to enjoy a magical mystery tour through increasingly obscure compiler errors. Document everything because if you do get it working, you won't be in a mood to do this twice.
MATLAB R2016b on Windows now supports the MinGW compiler. I'm successfully using this to compile code written primarily for Linux/gcc. I installed this from the Add-On menu in MATLAB (search MinGW).
For my case, I'm building with the legacy code tool. The only thing I needed to do differently than normal was to tell the compiler to support c99 via a compiler flag. This does the trick:
legacy_code('compile', def, {'CFLAGS=-std=c99'})
I had trouble getting the flag command just right (I had some extra quotes that apparently broke things), and asked The MathWorks, so credit is due to their support team for this.
If you are using mex, I would expect to do something very similar.
I would guess that the makefiles are irrelevant for your application; you will need to tell the mex or legacy_code function about all of the files necessary to build the whole application or link against pre-built libraries (which it sounds like you don't have).
I hope this helps!

How to compile single fortran objects using translator f2c?

I have the following problem: I want to simulate some control engineering system. As it is quite complicated I used the computer to derive some (complicated) equations which can only be exported to fortran77 code. On my development pc this is no problem (linux machine).
No I want to do it in hardware and here I have to use a windows OS. Unfortunately matlab does not support gfortran on windows. Therefore I can not compile the fortran files on that box.
My idea was now to translate to C as a C compiler is available at matlab on win. Any other (better) options?
I have several SUBROUTINES in the manner of the following.
SUBROUTINE sys_rhs (x, v, dx, param)
REAL*8 x(6)
C code is coming here
RETURN
END SUBROUTINE
Is it good practice and does it work to use the function sys_rhs__ in the interface function written in C?
The problem is, that I can not test it as I do not have massive access to the windows machine. So it should work if I try it and I should not need to experiment a lot.
Also what lib's do I need on windows? Where can I get them?
Thanks
Christian
Intel sells a pretty good fortran compiler (http://software.intel.com/en-us/articles/fortran-compilers/) which they used to give away for free for testing ... ;-)
PS No, Im not linked to this company in any way!
You might wanna check the gnumex project, which allows to compile MEX-files using the GNU GCC compiler suite (using either MinGW or Cygwin), with the ability to use g77 to compile Fortran programs.
The answer was quite simple: I had some syntax errors in my fortran code. gfortran does no such strict syntax checking and therefor interpreted the fortran code correctly. After removing the typos I succceded transcoding with f2c.
Thanks
there is a better solution to all this
use your linux machine or your windows machine to implement the function in c/c++ and then generate a dll file.
In matlab you can load the DLL and make a call to the functions inside it.
here is a simple tutorial that show how to call the dll with matlab.
http://www.mathworks.com/matlabcentral/fileexchange/15065-example-of-loading-c-dll-and-performance-comparison

c exec embeded in webpage

Does anyone know for a tool that allows a c executable to be run in the browser? I'm looking for a javascript, java, or flash solution because I don't have privileges to run c executables on the server.
The executables are basic input and output programs.
Looking at your comments, I hear you mention students and running simple programs. As a suggestion, you might want to look into CodePad. This will let you interpret simple C programs. Note that everything needs to be in one place, so you'll have to combine C and header files.
Here is a sample:
http://codepad.org/qQS31BwM
EDIT
Here's another one I found:
http://ideone.com/
When you run the program, at the bottom there is a link for input. You can use it to run the program with given input as entered.
You could use this as a basis for solving the problem:
http://bellard.org/jslinux/
Emscripten is an LLVM-to-JavaScript compiler. It takes LLVM bitcode (which can be generated from C/C++, using llvm-gcc or clang, or any other language that can be converted into LLVM) and compiles that into JavaScript, which can be run on the web (or anywhere else JavaScript can run).
Using Emscripten, you can
Compile C and C++ code into JavaScript and run that on the web
Run code in languages like Python as well, by compiling CPython from C to JavaScript and interpreting code in that on the web

Resources