Hi,
I'm trying to write a plugin that will call some hand-coded c functions in another file. It also needs to share variables via 'extern' with this other file. So in my plugin(class side) I have something like: declareCVarsIn: aCCodeGenerator aCCodeGenerator var: 'height' declareC: 'int height'. aCCodeGenerator var: 'width' declareC: 'int width' This generates, in C, static int height; static int width; If I understand this right, the 'static' keeps me from also making these extern, and also prevents me from sharing the other way by using 'extern' in the other file. I guess having lots of externs would clog up the namespace but for now I'd just like to get this to work. I see in the CCodeGenerator where the static is added, but I'm not sure what the proper way is to suppress this. Any hints? I am working in Squeak 3.7 (its the latest version for which I have managed to get VM generation going). Thanks, Eddie |
Hello Eddie,
there is Object>>cCode: or Object>>cCode:inSmalltalk:, see e.g. RePlugin>>allocateByteArrayAndSetRcvrPCREPtrFromPCRE: aPCREPtr for an example. With these constructs you can inject arbitrary C code into the generated C code. In RePlugin>>declareCVarsIn: you can see how to declare a header file, there is the line cg addHeaderFile:'"rePlugin.h"'. ; this header file is located in dir SomeBaseDir/platforms/Cross/plugins/RePlugin/ You could put the extern vars into a respective header file. Regards, Stephan On 12.01.2006 10:49, Eddie Cottongim wrote: > Hi, > > I'm trying to write a plugin that will call some hand-coded c functions > in another file. It also needs to share variables via 'extern' with this > other file. > > So in my plugin(class side) I have something like: > > declareCVarsIn: aCCodeGenerator > aCCodeGenerator var: 'height' declareC: 'int height'. > aCCodeGenerator var: 'width' declareC: 'int width' > > This generates, in C, > static int height; > static int width; > > If I understand this right, the 'static' keeps me from also making these > extern, and also prevents me from sharing the other way by using > 'extern' in the other file. I guess having lots of externs would clog up > the namespace but for now I'd just like to get this to work. > > I see in the CCodeGenerator where the static is added, but I'm not sure > what the proper way is to suppress this. Any hints? > > I am working in Squeak 3.7 (its the latest version for which I have > managed to get VM generation going). > > Thanks, > Eddie > > > -- Stephan Rudlof ([hidden email]) "Genius doesn't work on an assembly line basis. You can't simply say, 'Today I will be brilliant.'" -- Kirk, "The Ultimate Computer", stardate 4731.3 |
Some refinements:
On 12.01.2006 20:20, I wrote: > Hello Eddie, > > there is Object>>cCode: or Object>>cCode:inSmalltalk:, see e.g. > RePlugin>>allocateByteArrayAndSetRcvrPCREPtrFromPCRE: aPCREPtr > for an example. With these constructs you can inject arbitrary C code > into the generated C code. > In RePlugin>>declareCVarsIn: You should look into the corresponding Category to see what you have to do else. > you can see how to declare a header file, > there is the line > cg addHeaderFile:'"rePlugin.h"'. > ; this header file is located in dir > SomeBaseDir/platforms/Cross/plugins/RePlugin/ > You could put the extern vars into a respective header file. should be: You could put the *declarations* of the extern vars (definitions being in some *.c file) into a respective header file. Stephan > > > Regards, > Stephan > > > On 12.01.2006 10:49, Eddie Cottongim wrote: > >>Hi, >> >>I'm trying to write a plugin that will call some hand-coded c functions >>in another file. It also needs to share variables via 'extern' with this >>other file. >> >>So in my plugin(class side) I have something like: >> >>declareCVarsIn: aCCodeGenerator >>aCCodeGenerator var: 'height' declareC: 'int height'. >>aCCodeGenerator var: 'width' declareC: 'int width' >> >>This generates, in C, >>static int height; >>static int width; >> >>If I understand this right, the 'static' keeps me from also making these >>extern, and also prevents me from sharing the other way by using >>'extern' in the other file. I guess having lots of externs would clog up >>the namespace but for now I'd just like to get this to work. >> >>I see in the CCodeGenerator where the static is added, but I'm not sure >>what the proper way is to suppress this. Any hints? >> >>I am working in Squeak 3.7 (its the latest version for which I have >>managed to get VM generation going). >> >>Thanks, >>Eddie >> >> >> > > -- Stephan Rudlof ([hidden email]) "Genius doesn't work on an assembly line basis. You can't simply say, 'Today I will be brilliant.'" -- Kirk, "The Ultimate Computer", stardate 4731.3 |
Thanks Stephan. I tried using a header file and this worked. However, it
means I have to remove the Plugin's variable declarations so the translator doesn't generate a definition for them, which in turn means that I can't reference them from Slang at all (can only use cCode). I might try hacking the CCodeGenerator to put out the extern more directly. Eddie Stephan Rudlof wrote: >Some refinements: > >On 12.01.2006 20:20, I wrote: > > >>Hello Eddie, >> >>there is Object>>cCode: or Object>>cCode:inSmalltalk:, see e.g. >> RePlugin>>allocateByteArrayAndSetRcvrPCREPtrFromPCRE: aPCREPtr >>for an example. With these constructs you can inject arbitrary C code >>into the generated C code. >> >> > > > >>In RePlugin>>declareCVarsIn: >> >> > >You should look into the corresponding Category to see what you have to >do else. > > > >>you can see how to declare a header file, >>there is the line >> cg addHeaderFile:'"rePlugin.h"'. >>; this header file is located in dir >> SomeBaseDir/platforms/Cross/plugins/RePlugin/ >> >> > > > >>You could put the extern vars into a respective header file. >> >> > >should be: You could put the *declarations* of the extern vars >(definitions being in some *.c file) into a respective header file. > >Stephan > > |
Hello Eddie,
On 13.01.2006 06:15, Eddie Cottongim wrote: > Thanks Stephan. I tried using a header file and this worked. However, it > means I have to remove the Plugin's variable declarations so the > translator doesn't generate a definition for them, which in turn means > that I can't reference them from Slang at all (can only use cCode). I > might try hacking the CCodeGenerator to put out the extern more directly. A workaround: - define the static Plugin's variables as before, but with another name as the extern ones (rename the vars in one of these sets), and then - assign the extern to the static ones and vice versa via self cCode: 'pluginVar = externVar;' self cCode: 'externVar = pluginVar;' or self cCode: 'ptrToExternVar = &externVar;' . Then the hacked code is small and you are able to use Slang vars. Regards, Stephan > > Eddie > > Stephan Rudlof wrote: > > >>Some refinements: >> >>On 12.01.2006 20:20, I wrote: >> >> >> >>>Hello Eddie, >>> >>>there is Object>>cCode: or Object>>cCode:inSmalltalk:, see e.g. >>> RePlugin>>allocateByteArrayAndSetRcvrPCREPtrFromPCRE: aPCREPtr >>>for an example. With these constructs you can inject arbitrary C code >>>into the generated C code. >>> >>> >> >> >> >> >>>In RePlugin>>declareCVarsIn: >>> >>> >> >>You should look into the corresponding Category to see what you have to >>do else. >> >> >> >> >>>you can see how to declare a header file, >>>there is the line >>> cg addHeaderFile:'"rePlugin.h"'. >>>; this header file is located in dir >>> SomeBaseDir/platforms/Cross/plugins/RePlugin/ >>> >>> >> >> >> >> >>>You could put the extern vars into a respective header file. >>> >>> >> >>should be: You could put the *declarations* of the extern vars >>(definitions being in some *.c file) into a respective header file. >> >>Stephan >> >> > > > > -- Stephan Rudlof ([hidden email]) "Genius doesn't work on an assembly line basis. You can't simply say, 'Today I will be brilliant.'" -- Kirk, "The Ultimate Computer", stardate 4731.3 |
Hello Eddie,
On 13.01.2006 21:56, I wrote: >... > A workaround: > - define the static Plugin's variables as before, but with another name > as the extern ones (rename the vars in one of these sets), and then > - assign the extern to the static ones and vice versa via > self cCode: 'pluginVar = externVar;' > self cCode: 'externVar = pluginVar;' > or > self cCode: 'ptrToExternVar = &externVar;' > . > > Then the hacked code is small and you are able to use Slang vars. There is a *much* better possibility (has come into my mind inside a restaurant after writing down the stupid workaround above). What about writing accessor methods? getExternalVar self returnTypeC: 'int'. "or some other type" ^ self cCode: 'externalVar' "int in this case" setExternalVar: aValue self cCode: 'externalVar = aValue' "arg aValue has to be of same type as externalVar" Concrete examples are Mpeg3Plugin>>mpeg3tValueOf: (getter), and FilePlugin>>setMacFile:Type:AndCreator: (setter). Hope this helps, Stephan >... -- Stephan Rudlof ([hidden email]) "Genius doesn't work on an assembly line basis. You can't simply say, 'Today I will be brilliant.'" -- Kirk, "The Ultimate Computer", stardate 4731.3 |
PS:
On 14.01.2006 01:23, I wrote: >... > There is a *much* better possibility (has come into my mind inside a > restaurant after writing down the stupid workaround above). > > What about writing accessor methods? > > getExternalVar > self returnTypeC: 'int'. "or some other type" > ^ self cCode: 'externalVar' "int in this case" > > setExternalVar: aValue > self cCode: 'externalVar = aValue' "arg aValue has to be of same type > as externalVar" Such methods can be inlined, so don't be afraid of performance problems. --sr > > Concrete examples are > Mpeg3Plugin>>mpeg3tValueOf: (getter), and > FilePlugin>>setMacFile:Type:AndCreator: (setter). > > > Hope this helps, > Stephan > > >>... > > -- Stephan Rudlof ([hidden email]) "Genius doesn't work on an assembly line basis. You can't simply say, 'Today I will be brilliant.'" -- Kirk, "The Ultimate Computer", stardate 4731.3 |
Free forum by Nabble | Edit this page |