Author |
Message
|
Emil_halim Ziron Beta Tester
![avatar](http://www.ogre3d.org/forums/download/file.php?avatar=10720.gif)
(send private message)
Posts: 639 Topics: 104
Location: Alex, Egypt | [1122] #define plugin - posted: 2012-05-16 21:49:21
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 .
Code://******************************************************//
// //
// 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 |
Emil_halim Ziron Beta Tester
![avatar](http://www.ogre3d.org/forums/download/file.php?avatar=10720.gif)
(send private message)
Posts: 639 Topics: 104
Location: Alex, Egypt | [1179] - posted: 2012-05-21 18:02:52
Hi this is the final plugin of #define directive.
Code://******************************************************//
// //
// 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
Code://
//
// 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 Ziron Beta Tester
![avatar](http://www.ogre3d.org/forums/download/file.php?avatar=10720.gif)
(send private message)
Posts: 639 Topics: 104
Location: Alex, Egypt | [1180] - posted: 2012-05-21 18:33:11
I have changed this line
Code:H_strjoin(@buf ,'inline procedure ', @idnt, @par, '{' , @val , '}');
to
Code:H_strjoin(@buf ,'inline function ', @idnt, @par, '{' , @val , '$return eax }');
so that ziron accepts that
Code:print(addval(6,7) , '\r\n');
http://www.freewebs.com/ogremagic/index.htm |
Admin Site Admin
![avatar](http://www.codeziron.com/images/584.png)
(send private message)
Posts: 933 Topics: 55
Location: OverHertz Studio | [1182] - posted: 2012-05-21 19:38:45 This looks good, very good work! ![smile](images/smilies/yess.gif)
Download Ziron
Get free hosting for Ziron related fan-sites and Ziron projects, contact me in private message. |
Admin Site Admin
![avatar](http://www.codeziron.com/images/584.png)
(send private message)
Posts: 933 Topics: 55
Location: OverHertz Studio | [1183] - posted: 2012-05-21 19:50:50 btw can i ask why you do not use any of internal functions, such as the inbuild strlcpy etc?
using strlcpy is much faster:
![](uploads/strlcpy1.PNG)
Code: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);
Download Ziron
Get free hosting for Ziron related fan-sites and Ziron projects, contact me in private message. |
Emil_halim Ziron Beta Tester
![avatar](http://www.ogre3d.org/forums/download/file.php?avatar=10720.gif)
(send private message)
Posts: 639 Topics: 104
Location: Alex, Egypt | [1185] - posted: 2012-05-21 20:14:59
actually , i did not like to add the length as the third param.
i mean that , using this
Code:
is much easier and simple of this
Code: strcpy( dst , src , len);
http://www.freewebs.com/ogremagic/index.htm |
Admin Site Admin
![avatar](http://www.codeziron.com/images/584.png)
(send private message)
Posts: 933 Topics: 55
Location: OverHertz Studio | [1186] - posted: 2012-05-21 20:22:29 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 ![smile](images/smilies/smile.gif)
thanks for idea
Download Ziron
Get free hosting for Ziron related fan-sites and Ziron projects, contact me in private message. |
Admin Site Admin
![avatar](http://www.codeziron.com/images/584.png)
(send private message)
Posts: 933 Topics: 55
Location: OverHertz Studio | [1187] - posted: 2012-05-21 20:45:12 new function
Code://
// 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 ![smile](images/smilies/smile.gif)
Code: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);
![](uploads/strlcpy2.PNG)
i will make the strlcpy macro to be both strlcpy with len or without and also handle as strcpy for next release ![smile](images/smilies/smile.gif)
Download Ziron
Get free hosting for Ziron related fan-sites and Ziron projects, contact me in private message. |