Files
harbour-core/harbour/source/compiler/genhrb.c
Viktor Szakats b2594752b3 2008-08-19 14:58 UTC+0200 Viktor Szakats (harbour.01 syenar hu)
* make_deb.sh
     * Removed wrong comment recently added.
       (we will need to fix hbfimage compilation anyway)

   * harbour-ce-spec
   * harbour-w32-spec
   * harbour.spec
   * debian/control
   * include/vm.api
   * include/extend.api
   * include/button.ch
   * include/hbstack.h
   * include/item.api
   * include/rdd.api
   * include/gt.api
   * include/dbinfo.ch
   * include/error.api
   * include/filesys.api
   * include/hbapigt.h
   * include/clipdefs.h
   * include/assert.ch
   * include/fm.api
   * include/extend.h
   * source/compiler/genhrb.c
   * config/os2/install.cf
   * tests/db_brows.prg
   * tests/debugtst.prg
   * tests/ac_test.prg
   * tests/testhtml.prg
   * tests/tstcolor.prg
   * doc/hdr_tpl.txt
   * doc/man/harbour.1
   * doc/dirstruc.txt
   * doc/howtobld.txt
   * doc/en/diskspac.txt
   * doc/en/string.txt
   * doc/en/rddord.txt
   * doc/en/math.txt
   * doc/en/error.txt
   * doc/en/treport.txt
   * doc/en/memvar2.txt
   * doc/en/terminal.txt
   * doc/en/dir.txt
   * doc/en/rddmisc.txt
   * doc/en/var.txt
   * doc/en/nation.txt
   * doc/en/dbstrux.txt
   * doc/en/datetime.txt
   * doc/en/memo.txt
   * doc/en/tgetlist.txt
   * doc/en/tlabel.txt
   * doc/en/hb_compa.txt
   * doc/en/array.txt
   * doc/en/rdddb.txt
   * doc/en/dbsdf.txt
   * doc/en/tbrowse.txt
   * doc/en/hvm.txt
   * doc/en/input.txt
   * doc/en/dbdelim.txt
   * doc/en/browse.txt
   * doc/en/menu.txt
   * doc/en/file.txt
   * doc/en/binnum.txt
   * doc/en/tclass.txt
   * doc/en/set.txt
   * doc/en/misc.txt
   * doc/en/sayget.txt
   * doc/en/readme.txt
   * doc/es/diskspac.txt
   * doc/es/tlabel.txt
   * doc/es/array.txt
   * doc/es/dbsdf.txt
   * doc/es/tbrowse.txt
   * doc/es/math.txt
   * doc/es/hvm.txt
   * doc/es/treport.txt
   * doc/es/input.txt
   * doc/es/dbdelim.txt
   * doc/es/browse.txt
   * doc/es/memvar2.txt
   * doc/es/command.txt
   * doc/es/dir.txt
   * doc/es/file.txt
   * doc/es/eval.txt
   * doc/es/binnum.txt
   * doc/es/nation.txt
   * doc/es/var.txt
   * doc/es/tclass.txt
   * doc/es/dbstrux.txt
   * doc/es/misc.txt
   * doc/es/datetime.txt
   * doc/es/sayget.txt
   * doc/es/memo.txt
   * doc/es/readme.txt
   * doc/es/tgetlist.txt
   * doc/readme.txt
   * contrib/hbct/ctnet.c
   * contrib/hbct/video.c
   * contrib/hbct/dattime3.c
   * contrib/hbct/color.c
   * contrib/hbnf/acctyear.prg
   * contrib/hbnf/byteneg.prg
   * contrib/hbnf/isshare.prg
   * contrib/hbnf/sinkey.prg
   * contrib/hbnf/dhkey.h
   * contrib/hbnf/kspeed.c
   * contrib/hbnf/prtscr.c
   * contrib/hbnf/descendn.c
   * contrib/hbnf/peek.c
   * contrib/hbnf/scancode.prg
   * contrib/hbnf/vidmode.prg
   * contrib/hbnf/nwsem.prg
   * contrib/hbnf/acctadj.prg
   * contrib/hbnf/week.prg
   * contrib/hbnf/vidcur.prg
   * contrib/hbnf/iamidle.c
   * contrib/hbnf/prtesc.prg
   * contrib/hbnf/miltime.prg
   * contrib/hbnf/proper.c
   * contrib/hbnf/acctmnth.prg
   * contrib/hbnf/savearr.prg
   * contrib/hbnf/rand1.prg
   * contrib/hbnf/madd.prg
   * contrib/hbnf/dispmsg.prg
   * contrib/hbnf/settime.prg
   * contrib/hbnf/restsets.prg
   * contrib/hbnf/page.prg
   * contrib/hbnf/byt2bit.prg
   * contrib/hbnf/setkeys.c
   * contrib/hbnf/month.prg
   * contrib/hbnf/byt2hex.prg
   * contrib/hbnf/findith.prg
   * contrib/hbnf/at2.prg
   * contrib/hbnf/acctweek.prg
   * contrib/hbnf/dispc.c
   * contrib/hbnf/gcd.prg
   * contrib/hbnf/pegs.prg
   * contrib/hbnf/min2dhm.prg
   * contrib/hbnf/acctqtr.prg
   * contrib/hbnf/numlock.c
   * contrib/hbnf/dosver.prg
   * contrib/hbnf/nooccur.prg
   * contrib/hbnf/dayofyr.prg
   * contrib/hbnf/metaph.prg
   * contrib/hbnf/ontick.c
   * contrib/hbnf/menu1.prg
   * contrib/hbnf/byteand.prg
   * contrib/hbnf/sqzn.prg
   * contrib/hbnf/bytexor.prg
   * contrib/hbnf/ftidle.c
   * contrib/hbnf/workdays.prg
   * contrib/hbnf/byteor.prg
   * contrib/hbnf/asum.prg
   * contrib/hbnf/shift.c
   * contrib/hbnf/sleep.prg
   * contrib/hbnf/wda.prg
   * contrib/hbnf/aavg.prg
   * contrib/hbnf/woy.prg
   * contrib/hbnf/origin.c
   * contrib/hbnf/any2any.prg
   * contrib/hbnf/chdir.c
   * contrib/hbnf/adessort.prg
   * contrib/hbnf/setdate.prg
   * contrib/hbnf/netpv.prg
   * contrib/hbnf/amedian.prg
   * contrib/hbnf/blink.prg
   * contrib/hbnf/stod.c
   * contrib/hbnf/vertmenu.prg
   * contrib/hbnf/qtr.prg
   * contrib/hbnf/linked.prg
   * contrib/hbnf/n2color.c
   * contrib/hbnf/aredit.prg
   * contrib/hbnf/alt.c
   * contrib/hbnf/xbox.prg
   * contrib/hbnf/ftround.prg
   * contrib/hbnf/hex2dec.prg
   * contrib/hbnf/nwuid.prg
   * contrib/hbnf/dectobin.prg
   * contrib/hbnf/bitset.prg
   * contrib/hbnf/aemaxlen.prg
   * contrib/hbnf/nwlstat.prg
   * contrib/hbnf/invclr.prg
   * contrib/hbnf/ctrl.c
   * contrib/hbnf/lastday.prg
   * contrib/hbnf/tempfile.prg
   * contrib/hbnf/diskfunc.prg
   * contrib/hbnf/scregion.prg
   * contrib/hbnf/mouse2.prg
   * contrib/hbnf/d2e.prg
   * contrib/hbnf/ftisprn.c
   * contrib/hbnf/pickday.prg
   * contrib/hbnf/firstday.prg
   * contrib/hbnf/daytobow.prg
   * contrib/hbnf/anomatch.prg
   * contrib/hbnf/pvid.prg
   * contrib/hbnf/isbiton.prg
   * contrib/hbnf/color2n.c
   * contrib/hbnf/isbit.prg
   * contrib/hbnf/putkey.c
   * contrib/hbnf/cntryset.prg
   * contrib/hbnf/datecnfg.prg
   * contrib/hbnf/getenvrn.c
   * contrib/hbnf/easter.prg
   * contrib/hbnf/bytenot.prg
   * contrib/hbnf/ntow.prg
   * contrib/hbnf/poke.c
   * contrib/hbnf/aeminlen.prg
   * contrib/hbnf/floptst.prg
   * contrib/hbnf/pchr.prg
   * contrib/hbnf/savesets.prg
   * contrib/hbnf/mkdir.c
   * contrib/hbnf/year.prg
   * contrib/hbnf/caplock.c
   * contrib/hbnf/rmdir.c
   * contrib/hbnf/calendar.prg
   * contrib/hbnf/e2d.prg
   * contrib/hbnf/elapsed.prg
   * contrib/hbnf/sysmem.prg
   * contrib/hbnf/bitclr.prg
   * contrib/hbnf/eltime.prg
   * contrib/hbnf/aading.prg
   * contrib/hbnf/dfile.prg
   * contrib/hbnf/elapmil.prg
   * contrib/hbnf/setlastk.c
   * contrib/hbnf/pending.prg
   * contrib/hbnf/clrsel.prg
   * contrib/rddads/doc/en/adsfuncs.txt
   * contrib/rddads/doc/en/readme.txt
   * contrib/hbclip/hbclip.ch
   * contrib/hbclip/readme.txt
   * contrib/hbmisc/doc/en/ht_dbf.txt
   * utils/hbdoc/rtf.prg
   * utils/hbdoc/fclass1.prg
   * utils/hbdoc/genos2.prg
   * utils/hbdoc/genng.prg
   * utils/hbdoc/genasc.prg
   * utils/hbdoc/genchm.prg
   * utils/hbdoc/os2.prg
   * utils/hbdoc/ng.prg
   * utils/hbdoc/genhpc.prg
   * utils/hbdoc/html.prg
   * utils/hbdoc/ffile1.prg
   * utils/hbdoc/ft_funcs.prg
   * utils/hbdoc/hbdoc.prg
   * utils/hbdoc/troff.prg
   * utils/hbdoc/genhtm.prg
   * utils/hbdoc/genpdf1.prg
   * utils/hbdoc/gentrf.prg
   * utils/hbdoc/genrtf.prg
   * utils/hbmake/fclass1.prg
   * utils/hbmake/prb_stak.prg
   * utils/hbmake/ffile1.prg
   * utils/hbmake/ft_funcs.prg
   * utils/hbmake/pickfile.prg
   * utils/hbmake/hbmutils.prg
     * More cleanups, formatting (filename casing, CA-Cl*pper, etc).
2008-08-19 13:11:22 +00:00

178 lines
5.5 KiB
C

/*
* $Id$
*/
/*
* Harbour Project source code:
* Compiler Harbour Portable Object (.hrb) generation
*
* Copyright 1999 Eddie Runia <eddie@runia.com>
* www - http://www.harbour-project.org
*
* Copyright 2007 Przemyslaw Czerpak <druzus / at / priv.onet.pl>
* rewritten to work on memory buffers and with new compiler code
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA (or visit
* their web site at http://www.gnu.org/).
*
*/
#include "hbcomp.h"
#define SYM_NOLINK 0 /* symbol does not have to be linked */
#define SYM_FUNC 1 /* function defined in this module */
#define SYM_EXTERN 2 /* function defined in other module */
#define SYM_DEFERRED 3 /* lately bound function */
static PFUNCTION hb_compFirstFunc( HB_COMP_DECL )
{
PFUNCTION pFunc = HB_COMP_PARAM->functions.pFirst;
if( ! HB_COMP_PARAM->fStartProc )
pFunc = pFunc->pNext;
return pFunc;
}
static ULONG hb_compHrbSize( HB_COMP_DECL, ULONG * pulSymbols, ULONG * pulFunctions )
{
PFUNCTION pFunc;
PCOMSYMBOL pSym;
ULONG ulSize;
* pulSymbols = * pulFunctions = 0;
/* count total size */
ulSize = 10; /* signature[4] + version[2] + symbols_number[4] */
pSym = HB_COMP_PARAM->symbols.pFirst;
while( pSym )
{
( * pulSymbols )++;
ulSize += strlen( pSym->szName ) + 3; /* \0 + symscope[1] + symtype[1] */
pSym = pSym->pNext;
}
ulSize += 4; /* functions_number[4] */
/* Generate functions data */
pFunc = hb_compFirstFunc( HB_COMP_PARAM );
while( pFunc )
{
( * pulFunctions )++;
ulSize += strlen( pFunc->szName ) + 5 + pFunc->lPCodePos; /* \0 + func_size[4] + function_body */
pFunc = pFunc->pNext;
}
return ulSize;
}
void hb_compGenBufPortObj( HB_COMP_DECL, BYTE ** pBufPtr, ULONG * pulSize )
{
PFUNCTION pFunc;
PCOMSYMBOL pSym;
ULONG ulSymbols, ulFunctions, ulLen;
BYTE * ptr;
* pulSize = hb_compHrbSize( HB_COMP_PARAM, &ulSymbols, &ulFunctions );
/* additional 0 byte is for passing buffer directly as string item */
ptr = * pBufPtr = ( BYTE * ) hb_xgrab( * pulSize + 1 );
/* signature */
*ptr++ = 0xC0;
*ptr++ = 'H';
*ptr++ = 'R';
*ptr++ = 'B';
HB_PUT_LE_UINT16( ptr, 2 ); /* version number */
ptr += 2;
HB_PUT_LE_UINT32( ptr, ulSymbols ); /* number of symbols */
ptr += 4;
/* generate the symbol table */
pSym = HB_COMP_PARAM->symbols.pFirst;
while( pSym )
{
ulLen = strlen( pSym->szName ) + 1;
memcpy( ptr, pSym->szName, ulLen );
ptr += ulLen;
/* TOFIX: this conversion strips upper byte from symbol scope
* Now we added workaround for it by using some strict
* bit order and restoring some others at runtime when
* .hrb file is loaded but we should create new format
* for .hrb files in which this field will have at least
* 16bit [druzus]
*/
*ptr++ = ( BYTE ) pSym->cScope;
/* symbol type */
/* if( hb_compFunctionFind( HB_COMP_PARAM, pSym->szName ) ) */
if( pSym->cScope & HB_FS_LOCAL )
*ptr++ = SYM_FUNC; /* function defined in this module */
else if( pSym->cScope & HB_FS_DEFERRED )
*ptr++ = SYM_DEFERRED; /* lately bound function */
else if( hb_compFunCallFind( HB_COMP_PARAM, pSym->szName ) )
*ptr++ = SYM_EXTERN; /* external function */
else
*ptr++ = SYM_NOLINK; /* other symbol */
pSym = pSym->pNext;
}
HB_PUT_LE_UINT32( ptr, ulFunctions ); /* number of functions */
ptr += 4;
/* generate functions data */
pFunc = hb_compFirstFunc( HB_COMP_PARAM );
while( pFunc )
{
ulLen = strlen( pFunc->szName ) + 1;
memcpy( ptr, pFunc->szName, ulLen );
ptr += ulLen;
HB_PUT_LE_UINT32( ptr, pFunc->lPCodePos ); /* function size */
ptr += 4;
memcpy( ptr, pFunc->pCode, pFunc->lPCodePos ); /* function body */
ptr += pFunc->lPCodePos;
pFunc = pFunc->pNext;
}
}
void hb_compGenPortObj( HB_COMP_DECL, PHB_FNAME pFileName )
{
char szFileName[ _POSIX_PATH_MAX + 1 ];
ULONG ulSize;
BYTE * pHrbBody;
FILE * yyc;
if( ! pFileName->szExtension )
pFileName->szExtension = ".hrb";
hb_fsFNameMerge( szFileName, pFileName );
yyc = hb_fopen( szFileName, "wb" );
if( ! yyc )
{
hb_compGenError( HB_COMP_PARAM, hb_comp_szErrors, 'E', HB_COMP_ERR_CREATE_OUTPUT, szFileName, NULL );
return;
}
if( ! HB_COMP_PARAM->fQuiet )
{
char buffer[ 80 + _POSIX_PATH_MAX ];
snprintf( buffer, sizeof( buffer ),
"Generating Harbour Portable Object source output to \'%s\'... ", szFileName );
hb_compOutStd( HB_COMP_PARAM, buffer );
}
hb_compGenBufPortObj( HB_COMP_PARAM, &pHrbBody, &ulSize );
fwrite( pHrbBody, ulSize, 1, yyc );
hb_xfree( pHrbBody );
fclose( yyc );
if( ! HB_COMP_PARAM->fQuiet )
hb_compOutStd( HB_COMP_PARAM, "Done.\n" );
}