Thursday, November 2, 2023

antolepore.blogspot.com disappeared


antolepore.blogspot.com disappeared

I had my account blocked by mistake and when they gave me my antoleporeATgmailDOTcom account back but today I noticed that my BLOG disappeared

http://antolepore.blogspot.com/

How should I do it?

I can't recreate it from scratch and there was no option to make a backup!

Help me!!!
How should I do it?

Wednesday, September 26, 2012

The best LLVM tutorial


http://adrianboeing.blogspot.com/2009/05/llvm-for-beginners-windows.html

Saturday, May 30, 2009

LLVM for Beginners (Windows)

Low Level Virtual Machine (LLVM) is, officially, "Compiler Infrastructure". In my own words, it's a platform independent optimizing assembler. That is, you write high level assembly code for a virtual machine, and then LLVM goes through it, optimizes it, and pumps out the native assembly for your target platform (e.g. x86). The best part is that it can do it Just-In-Time (JIT) too!

LLVM and Clang (the llvm c compiler frontend) first gained my interest when I was looking into OpenCL, and heard Apple's technology of choice was LLVM. After that, LLVM replaced GCC for compiling FreeBSD, and more recently, Rapidmind have sung LLVM's praise. Could LLVM be the future for high performance languages? (Goodbye CUDA/Open64?)

So, how do you get started? First, download LLVM, I just grabbed the MinGW builds for Windows. Now, we need a compiler (LLVM is just the 'assembler', Clang is the C front end). LLVM provide anonline compiler. Copy past your code in there, for example:
#include
int main() {
printf("hello world\n");
return 0;
}

The online compiler will then spit out the human readable LLVM assembly code, or 'll' code. Copy and paste this to a file, e.g. "hello.ll".
Now we can turn this into a binary bytecode representation usingthe LLVM assembler, llvm-as:
llvm-as hello.ll
This will generate hello.bc, the byte code version of the file.

We can either run this JIT with lli, (just type lli hello.bc) or create an assembly file with llc the LLVM static compiler. If you wish to compile the program for windows, you need to change the target provided by the online compiler to a windows compatible compiler. Do this by replacing the 'target triple = "i386-pc-linux-gnu"' line in your ll file to 'target triple = "i586-mingw32msvc"'. (Thanks Eli!) Re-run the assmber, Now your ready to compile:
llc hello.bc
Which generates hello.s

At this point, you need to use your platforms compiler, for example MinGW's gcc:
C:\MinGW\bin>gcc hello.s
C:\MinGW\bin>a.exe
hello world


Cool! The LLVM community is very open and friendly so I'm looking foward to trying some nifty things with LLVM!

1 comment:

kalyan said...
This comment has been removed by the author.

Monday, September 24, 2012

String storage allocation tutorial

This program extracted from C tutorial by Mark  Burgess didn't completely work in Mingw gcc v 4.5.2.
It worked up to the last printf statement.
I don't understand it but it shows the incompatibilities among different C compilers.

/******************************************************/
/*                                                    */
/* String storage allocation                          */
/*                                                    */
/******************************************************/

#include <stdio.h>

/* #include another file for malloc() and   */
/* strlen() ???. Check the compiler manual! */

#define NOOFSTR   3
#define BUFSIZE   255
#define CODE      0

/******************************************************/
/* Level 0                                            */
/******************************************************/


main ()

{ char *array[NOOFSTR], *malloc();
  char buffer[BUFSIZE];
  int i;

printf ("Total no of string to enter=%d\n",NOOFSTR);
for (i = 0; NOOFSTR ; i++)
   {
   printf ("Enter string %d :",i);
   scanf  ("%255s",buffer);

   array[i] = malloc(strlen(buffer)+1);

   if (array[i] == NULL)
      {
      printf ("Can't allocate memory\n");
      QuitSafely (array);
      }

   strcpy (array[i],buffer);
   printf ("%s\n",array[i]);
   }
printf ("%s\n",array[0]);
/*
for (i = 0; i < NOOFSTR; i++)
   {
   printf ("%s\n",array[i]);
   }
*/
QuitSafely(array);
}

/******************************************************/
/* Snakes & Ladders!                                  */
/******************************************************/

QuitSafely (array)       /* Quit & de-alloc memory */

char *array[NOOFSTR];

{ int i, len;

for (i = 0; i < NOOFSTR; i++)
   {
   len = strlen(array[i]) + 1;
    free(array[i]);
/*
    gcc free is of void type so cannot return any value.
    if (free (array[i]) != 0)
      {
      printf ("Debug: free failed\n");
      }
*/
      }

exit (CODE);
}

               /* end */

Sunday, September 16, 2012

Best Tutorial on Flex and Bison

http://aquamentus.com/flex_bison.html
Chris verBurg 2011-12-08

I had just finished all the tutorials and manged to make it work with mingw64 and gnuwin32, latest version as 16 Sep 1963. Malaysia day.

This is the easiest way of writing your own assemblers and compilers. These languages can be incorporated into application software since the output is just C or C++ source codes.

The only problem compiling was the missing yywrap library. Fixing it by including its source into the flex source file solved the problem. Take out the -lfa option in flex.

The make file for windows environment didn't work so had to resort to batch programming.

File: build_snazzle.bat

bison -d snazzle.y
flex snazzle.l
g++ snazzle.tab.c lex.yy.c  -o snazzle.exe
snazzle
pause
del lex.yy.c snazzle.tab.c snazzle.tab.h

Flex source File: snazzle.l

/* File snazzle.l
w w w . a q u a m e n t u s . c o m Chris verBurg 2011-12-08 */

/* Note that flex cannot take // as comment line here */

%{
#include <iostream>
#include "snazzle.tab.h"
using namespace std;
#define YY_DECL extern "C" int yylex()
int line_num = 1;
%}
%%
[ \t] ;
sNaZZle        { return SNAZZLE; }
type           { return TYPE; }
end            { return END; }
[0-9]+\.[0-9]+ { yylval.fval = atof(yytext); return FLOAT; }
[0-9]+         { yylval.ival = atoi(yytext); return INT; }
[a-zA-Z0-9]+   {
    // we have to copy because we can't rely on yytext not changing underneath us:
    yylval.sval = strdup(yytext);
    return STRING;
}
\n             { ++line_num; return ENDL;}
.              ;
%%

// Not in the original file. It is taken from gnu flex sources. Othman Ahmad
int     yywrap (void)
{
    return 1;
}

Bison source File: snazzle.y

// File snazzle.y  from w w w . a q u a m e n t u s . c o m Chris verBurg 2011-12-08
%{
#include <cstdio>
#include <iostream>
using namespace std;

//Included in the original file, but will cause errors if included. Othman Ahmad
//#include "y.tab.h"  // to get the token types that we return

// stuff from flex that bison needs to know about:
extern "C" int yylex();
extern "C" int yyparse();
extern "C" FILE *yyin;
extern int line_num;

void yyerror(const char *s);
%}

// Bison fundamentally works by asking flex to get the next token, which it
// returns as an object of type "yystype".  But tokens could be of any
// arbitrary data type!  So we deal with that in Bison by defining a C union
// holding each of the types of tokens that Flex could return, and have Bison
// use that union instead of "int" for the definition of "yystype":
%union {
    int ival;
    float fval;
    char *sval;
}

// define the constant-string tokens:
%token SNAZZLE TYPE
%token END ENDL

// define the "terminal symbol" token types I'm going to use (in CAPS
// by convention), and associate each with a field of the union:
%token <ival> INT
%token <fval> FLOAT
%token <sval> STRING

%%

// the first rule defined is the highest-level rule, which in our
// case is just the concept of a whole "snazzle file":
snazzle:
    header template body_section footer { cout << "done with a snazzle file!" << endl; }
    ;
header:
    SNAZZLE FLOAT ENDLS{ cout << "reading a snazzle file version " << $2 << endl; }
    ;
template:
    typelines
    ;
typelines:
    typelines typeline
    | typeline
    ;
typeline:
    TYPE STRING ENDLS{ cout << "new defined snazzle type: " << $2 << endl; }
    ;
body_section:
    body_lines
    ;
body_lines:
    body_lines body_line
    | body_line
    ;
body_line:
    INT INT INT INT STRING ENDLS{ cout << "new snazzle: " << $1 << $2 << $3 << $4 << $5 << endl; }
    ;
footer:
    END ENDLS
    ;
ENDLS:
    ENDLS ENDL
    | ENDL
    ;
%%

main() {
    // open a file handle to a particular file:
    FILE *myfile = fopen("in.snazzle", "r");
    // make sure it's valid:
    if (!myfile) {
        cout << "I can't open a.snazzle.file!" << endl;
        return -1;
    }
    // set flex to read from it instead of defaulting to STDIN:
    yyin = myfile;

    // parse through the input until there is no more:
    do {
        yyparse();
    } while (!feof(yyin));
   
}

void yyerror(const char *s) {
    cout << "EEK, parse error on line " << line_num << "! Message: " << s << endl;
    // might as well halt now:
    exit(-1);
}

File for testing: in.snazzle

sNaZZle 1.3
type foo
type bar
type bas
0 0 10 5 foo
20 10 30 20 foo
type oops
7 8 12 13 bas
78 124 100 256 bar
end



Saturday, September 15, 2012

Re: The simplest High Level Language

http://aquamentus.com/flex_bison.html

This is a good tutorial on flex and bison. I had problems making flex
work. It misses one file, requiring yywrap() function.

At the moment I had to write my own yylex() fuction as described by
the Bison examples.
I managed to get flex for gnuwin32 to work by adding this function to
the flex source:

int yywrap (void)
{
return 1;
}
It worked.
But on trying to extend it to file input, I can't seem to set the yyin
global variable.

Thursday, September 13, 2012

Re: The simplest Assembler

I had to refresh my C programming skills. I used these codes copied
outright, and some modified from the internet sources. Sorry, I cannot
locate them partly because of the lack of copyright notices within the
codes. These are simple testing codes anyway.
Copied to my blog on programming. Hopefully I will not forget it next
time.

I thought I don't need it because I am supposed to be able to remember
it easy. I was wrong. You need it to test your C installation.
helloworld.c
#include <stdio.h>
main()
{
printf("hello, world");
}

Reading manuals just boring and time consuming. Testing is better.

/* Testing main() */
#include <stdio.h>
int main(int argc, const char* argv[])
{
int i;
printf("argc=%d\n",argc);
for(i=0; i<argc; i++){
printf("i=%d, argv=%s\n",i,argv[i]); };
}

The original was from a C tutorial site, but I modified it so that I
can use arguments to put the file names, instead of embedding the
filenames in the source codes.
You can have stdin(your text screen) for input, or stdout(for screen
output). These work in console, or text screen of Windows.


// copy file from first argument to second argument
#include <stdio.h>
int main(int argc, const char* argv[]) {
FILE *fin;
FILE *fout;
int c;
fin =fopen(argv[1], "r"); fout =fopen(argv[2],"w");

if (fin != NULL) {
if (fout != NULL) {
c = fgetc (fin);
while (c >= 0) {
fputc (c, fout);
c = fgetc (fin);
}
fclose (fout);
} else {
fprintf (stderr, "Cannot write to output file");
}
fclose (fin);
} else {
fprintf (stderr, "Cannot read from input file");
}
}

With all these test files, I was able to improve Bison examples so
that it can read from files instead of just stdin, as interpreters. It
took a week of my time.
Gnu does not make this source available. In order to save people's
time, I shall upload it in this thread, and copied to my
microprocessor development blog.