Skip to content
Low-level programming · Higher-level thinking
Login Register
Forum / Plugins

#define plugin

A community discussion post from the original code:zIRON forum.

Emil_halim member
Posts
639
Threads
104
Location
Alex, Egypt

#define plugin

Hi all

here is a quick and simple pluging that manipulate a c_like #define statement.

i was converting a h file header and i 've wrote this plugin instead of converting.

may be it will help someone .

//******************************************************//
//                                                      //
//    Ziron Plugin                                      //
//    Define (Define.zir)                               //
//                                                      //
//    by Emil halim  16/5/2012                          //
//                                                      //
//******************************************************//

plugins off;

program WIN32DLL 'Define PLUGIN';

#include 'zirplug/framework.zir';

//////////////////////////////

const strLoaded = 'define plugin has loaded!';

//////////////////////////////


function  H_strcpy(Dword dst,src)
{ 
  uses edx ebx;
  Eax = dst; 
  Edx = src; 
  push Eax;
  BL =  char[Edx]; 
  while ( BL != 0)  
   { 
      char[Eax] = BL; 
      Eax++; Edx++; 
      BL =  char[Edx]; 
   }  
  char[Eax] = 0;
  pop Eax;
} 

function H_strcat(Dword dst,src) 
{ 
  uses edx ebx;
  Eax = dst; 
  Edx = src;
  push Eax;
  while (byte[Eax] != 0) { Eax++; } 
  while (byte[Edx] != 0)  
   { 
      BL =  char[Edx]; 
      char[Eax] = BL;  
      Edx++; Eax++; 
   } 
  char[Eax] = 0;
  pop Eax;
} 

inline procedure  H_strjoin(;)
{
  $temp = 1;
  $to = $argc - 1;  
  $repeat $to:  
     H_strcat($arg[0],$arg[$temp]);  
  $temp++;
  $end
}

function event_Define() 
{
    uses edi esi ecx ebx;
    char  buf[2048];
    char  idnt[255];
    char  val[255];
       
    eax = Ziron_ExtractLine(@buf,2047);
    Ziron_ShowMessage(@buf); 
      
    eax = Ziron_ExpectNextToken(zirIdent); 
    if (eax == -1) {
        Ziron_FatalError('Expected identfire Name');
        return true;
    }
    H_strcpy(@idnt, Ziron_GetStringValue());
    
    eax = Ziron_GetNextToken();
    if( eax == zirHexadecimal)
     {  
        H_strcpy(@val, '0x');
        H_strcat(@val, Ziron_GetStringValue());
     }
    else
     {
       H_strcpy(@val, Ziron_GetStringValue());
     }    
    
    buf=0;    
    H_strjoin(@buf ,'const ', @idnt, ' = ', @val , ';');
    
    Ziron_ShowMessage(@buf);
    Ziron_Exec(@buf); 
    
    return true; 
}



procedure FileEntry(DWord handle; char* strCode) {
  
     uses edi;
     eax = strOffset(strCode, '#define');
     while (char[eax] <> 0) {
            char[eax] = "_";
            eax = strOffset(eax, '#define');
     }
    
}

function InitPlugin(pointer pGetF) {
  Ziron_LoadAll(pGetF); //use framework utility function to load all

  //register our keyword
  Ziron_RegisterKeyword('_define', @event_Define);
  
  return strLoaded;
}

procedure UnloadPlugin()
{
   
}

entry function DLLMain(DWord iDLL; DWord iReason; Pointer iResult) {
  if (iReason == DLL_PROCESS_ATTACH) {
    return true;
  }

  return false;
}

exports InitPlugin, UnloadPlugin, FileEntry;      

have fun.

http://www.freewebs.com/ogremagic/index.htm

Replies

Emil_halim member
Posts
639
Threads
104
Location
Alex, Egypt

Hi this is the final plugin of #define directive.

//******************************************************//
//                                                      //
//    Ziron Plugin                                      //
//    Define (Define.zir)   ver   1.0                   //
//                                                      //
//    by Emil halim  21/5/2012                          //
//                                                      //
//******************************************************//

plugins off;

program WIN32DLL 'Define PLUGIN';

#include 'zirplug/framework.zir';

//////////////////////////////

const strLoaded = 'define plugin has loaded!';

//////////////////////////////


function  H_strcpy(Dword dst,src)
{ 
  uses edx ebx;
  Eax = dst; 
  Edx = src; 
  push Eax;
  BL =  char[Edx]; 
  while ( BL != 0)  
   { 
      char[Eax] = BL; 
      Eax++; Edx++; 
      BL =  char[Edx]; 
   }  
  char[Eax] = 0;
  pop Eax;
} 

function H_strcat(Dword dst,src) 
{ 
  uses edx ebx;
  Eax = dst; 
  Edx = src;
  push Eax;
  while (byte[Eax] != 0) { Eax++; } 
  while (byte[Edx] != 0)  
   { 
      BL =  char[Edx]; 
      char[Eax] = BL;  
      Edx++; Eax++; 
   } 
  char[Eax] = 0;
  pop Eax;
} 

inline procedure  H_strjoin(;)
{
  $temp = 1;
  $to = $argc - 1;  
  $repeat $to:  
     H_strcat($arg[0],$arg[$temp]);  
  $temp++;
  $end
}

function event_Define() 
{
    uses edi esi ecx ebx;
    char  buf[2048];
    char  idnt[255];
    char  val[1024];
    char  par[1024];
    char  parArr[10240];
    dword cnt;
                
    eax = Ziron_ExpectNextToken(zirIdent); 
    if (eax == -1) {
        Ziron_FatalError('Expected identfire Name');
        return true;
    }
    H_strcpy(@idnt, Ziron_GetStringValue());
    
    eax = Ziron_GetNextToken();
    if(eax == zirParenOpen) // function like macro
     {
          H_strcpy(@par, '(');
          ebx = Ziron_GetNextToken();
          esi = 0; 
          edi = @parArr;
          while( ebx != zirParenClose)
           {
              H_strcat(@par, Ziron_GetStringValue());
              if(ebx != zirComma)
               { 
                  H_strcpy( edi,Ziron_GetStringValue());
                  edi += 128; 
                  esi++;
               }   
              ebx = Ziron_GetNextToken();          
           }
          MOV cnt,esi 
          H_strcat(@par, ')');  
               
          val = 0;
          eax = Ziron_IsNextLineBreak();
          while(!eax)
           {
               Ziron_GetNextToken(); 
               eax = Ziron_GetStringValue();
               // if it has an other line
               if(char[eax] == "\\") // '\'  ascii=92
                 {
                  H_strcat(@val, '\r\n'); 
                  edi = Ziron_GetNextToken(); 
                }
                
               // adding '$' to param
               edi = cnt;
               ebx = @parArr;
               while(edi>0)
                { 
                   if(strCmp(ebx, Ziron_GetStringValue()))    
                    {
                      H_strcat(@val,' $'); 
                      break; 
                    }     
                   ebx += 128;       
                   edi--;
                }
              
               H_strcat(@val, Ziron_GetStringValue()); 
               H_strcat(@val, ' ');
               eax = Ziron_IsNextLineBreak();    
           }
          buf = 0;
          H_strjoin(@buf ,'inline procedure ', @idnt, @par, '{' , @val , '}');
          Ziron_ShowMessage(@buf); 
          Ziron_Exec(@buf); 
     }
     
    elseif(eax == zirSemiColon)  // it is ziron #define
     {
          buf=0;    
          H_strjoin(@buf ,'#define ', @idnt, ';');
          Ziron_Exec(@buf); 
     }
      
    else  // it is simple #define
     { 
        if( eax == zirHexadecimal) 
         {  
            H_strcpy(@val, '0x');
            H_strcat(@val, Ziron_GetStringValue());
         }
        else
         {
            H_strcpy(@val, Ziron_GetStringValue());
            eax = Ziron_IsNextLineBreak();
            while(!eax)
             {
                  Ziron_GetNextToken(); 
                  edx = Ziron_GetStringValue();
                  H_strjoin(@val, ' ' ,edx);
                  eax = Ziron_IsNextLineBreak();    
             } 
         }    
        buf=0;    
        H_strjoin(@buf ,'const ', @idnt, ' = ', @val , ';');
        Ziron_Exec(@buf); 
     }
       
    return true; 
}



procedure FileEntry(DWord handle; char* strCode) {
  
     uses edi;
     eax = strOffset(strCode, '#define');
     while (char[eax] <> 0) {
            char[eax] = "_";
            eax = strOffset(eax, '#define');
     }
    
}

function InitPlugin(pointer pGetF) {
  Ziron_LoadAll(pGetF); //use framework utility function to load all

  //register our keyword
  Ziron_RegisterKeyword('_define', @event_Define);
  
  return strLoaded;
}

procedure UnloadPlugin()
{
   
}

entry function DLLMain(DWord iDLL; DWord iReason; Pointer iResult) {
  if (iReason == DLL_PROCESS_ATTACH) {
    return true;
  }

  return false;
}

exports InitPlugin, UnloadPlugin, FileEntry;      

and here is a test program

//
//
//   test #Define directive
//
//       21-5-2012
//
//     By Emil Halim
//     

program WIN32CUI 'test #define';

#include 'console.zir';
#include 'zirutils.zir';


imports('msvcrt.dll') 
{
  //function printf(char* format[]): Int32; cdecl;
  function   rand(): int32; cdecl;
  procedure  srand( dword seed ); cdecl;
}


#define addval(a,b)  eax=a; eax+=b; 

#define getrandom(min, max) \
        ebx=max; ebx++; sub ebx,min  \
        eax=rand(); div ebx; XCHG eax,edx; eax+=min;   
    
#define   E_ACCESSDENIED      0x80070005

#define   DoFlag  1 shr 4                        /* this is comment */

#define USE_BACKSPACE;     


srand( GetTickCount() );

addval(6,7);
print(eax , '\r\n');

getrandom(12, 25);
print(eax , '\r\n');
   
wait_key();

ExitProcess(0);               

have fun.

http://www.freewebs.com/ogremagic/index.htm

Emil_halim member
Posts
639
Threads
104
Location
Alex, Egypt

I have changed this line

H_strjoin(@buf ,'inline procedure ', @idnt, @par, '{' , @val , '}');

to

H_strjoin(@buf ,'inline function ', @idnt, @par, '{' , @val , '$return eax }');

so that ziron accepts that

print(addval(6,7) , '\r\n');

http://www.freewebs.com/ogremagic/index.htm

Colin admin
Posts
928
Threads
49
Location
Bulgaria

This looks good, very good work! :yess:

Colin admin
Posts
928
Threads
49
Location
Bulgaria

btw can i ask why you do not use any of internal functions, such as the inbuild strlcpy etc?

using strlcpy is much faster:

program WIN32CUI 'Test';

#include 'ch.zir';

function  H_strcpy(Dword dst,src)
{ 
  uses edx ebx;
  Eax = dst; 
  Edx = src; 
  push Eax;
  BL =  char[Edx]; 
  while ( BL != 0)  
   { 
      char[Eax] = BL; 
      Eax++; Edx++; 
      BL =  char[Edx]; 
   }  
  char[Eax] = 0;
  pop Eax;
} 

char str1[8192];
char str2[8192];

//fill buf 1 with 8191 A's and end null char for testing
m_strFill(@str1, 8191, "A");

////////////

edi = GetTickCount();
for (esi = 1 to 500000) {
  H_strcpy(@str2, @str1);
}
eax = GetTickCount();
eax -= edi;

print('H_strcpy (Emil_halim): ', eax, 'ms\r\n');

////////////

#mmx off;

edi = GetTickCount();
for (esi = 1 to 500000) {
  strlcpy(@str2, @str1, 8192);
}
eax = GetTickCount();
eax -= edi;

print('strlcpy (mmx off): ', eax, 'ms\r\n');

//////////

#mmx on;

edi = GetTickCount();
for (esi = 1 to 500000) {
  strlcpy(@str2, @str1, 8192);
}
eax = GetTickCount();
eax -= edi;

print('strlcpy (mmx on): ', eax, 'ms\r\n');

wait_key(0);
ExitProcess(0);
Emil_halim member
Posts
639
Threads
104
Location
Alex, Egypt

actually , i did not like to add the length as the third param.

i mean that , using this

  strcpy( dst , src);

is much easier and simple of this

  strcpy( dst , src , len);

http://www.freewebs.com/ogremagic/index.htm

Colin admin
Posts
928
Threads
49
Location
Bulgaria

Hmm actually that is a good point since checking strlen will also make it slower, maybe i need to add len as optional and write new function for handling without len :)

thanks for idea

Colin admin
Posts
928
Threads
49
Location
Bulgaria

new function

//
// Copyright (c) OverHertz Ltd. 2012
//
function strCpy(char* dst, src) { 
	uses esi edi
	edi = dst;	
	esi = src; 

	push edi	
	repeat {
		al = [esi];
		[edi] = al;
		
		esi += 1;
		edi += 1;
	} until (al == 0);
	pop eax
} 

will add it to the library, it so far is very fast performer :)

program WIN32CUI 'Test';

#include 'ch.zir';

//
// Copyright (c) OverHertz Ltd. 2012
//
function strCpy(char* dst, src) { 
  uses esi edi
  edi = dst;  
  esi = src; 

  push edi  
  repeat {
    al = [esi];
    [edi] = al;
    
    esi += 1;
    edi += 1;
  } until (al == 0);
  pop eax
} 

//
// Copyright (c) Emil_halim
//
function  H_strcpy(Dword dst,src)
{ 
  uses edx ebx;
  Eax = dst; 
  Edx = src; 
  push Eax;
  BL =  char[Edx]; 
  while ( BL != 0)  
   { 
      char[Eax] = BL; 
      Eax++; Edx++; 
      BL =  char[Edx]; 
   }  
  char[Eax] = 0;
  pop Eax;
} 

char str1[8192];
char str2[8192];


//fill buf 1 with 8191 A's and end null char for testing
m_strFill(@str1, 8191, "A");

////////////

edi = GetTickCount();
for (esi = 1 to 500000) {
	strcpy(@str2, @str1);
}
eax = GetTickCount();
eax -= edi;

print('strcpy: ', eax, 'ms\r\n');

////////////

edi = GetTickCount();
for (esi = 1 to 500000) {
  H_strcpy(@str2, @str1);
}
eax = GetTickCount();
eax -= edi;

print('H_strcpy (Emil_halim): ', eax, 'ms\r\n');

////////////


#mmx off;

edi = GetTickCount();
for (esi = 1 to 500000) {
	strlcpy(@str2, @str1, 8192);
}
eax = GetTickCount();
eax -= edi;

print('strlcpy (mmx off): ', eax, 'ms\r\n');

//////////

#mmx on;

edi = GetTickCount();
for (esi = 1 to 500000) {
	strlcpy(@str2, @str1, 8192);
}
eax = GetTickCount();
eax -= edi;

print('strlcpy (mmx on): ', eax, 'ms\r\n');

//////////

wait_key(0);
    
ExitProcess(0);

i will make the strlcpy macro to be both strlcpy with len or without and also handle as strcpy for next release :)