0

Are there any static code analyzing tools for C that convert pointers to arrays of a static size? It is a marginal pain and I just figured I'd check with the community. A quick google search did not turn up anything useful because "to" in the phrase "pointer to array" generally means something completely different from what I'm looking for here... Any suggestions or keywords would be helpful.

P.s. I'm trying to convert some code into synthesizable C for a hardware implementation.

So for instance, this code is not synthesizable:

void sum(float *a, int size, float *out) {
    int i;
    for (i=0;i<size;i++)
        *out += a+1;
}

It is not synthesizable since the size of a is not statically declared at runtime. (Note, that out should be ok since it is not an array - though I'm not 100% sure on that.) It would be lovely if there was a tool to convert use of variable sized arrays to arrays whose sizes were known at compile time.

Furthermore, this code must be transformed (currently manually) to something like this:

// Not 100% sure if float * is still allowed.
void sum(float a[SIZE], float *out) {
    int i;
    for (i=0;i<SIZE;i++)
        *out += a[i]; // Not sure if this is necessary.
}

Right now, I would have to do this manually. I have to resolve all usages of dynamic arrays. This is how HLS works. It is a serious issue. It is hard (or at least time-consuming) to do when you did not write the original code. It must be converted so that you can synthesize the code to hardware.

All array sizes must be resolved - they may not have been explicitly declared initially. Then these array sizes must be propagated to all affected functions/references.

Finally, for those of you confused by "synthesizable", feel free to check out these two links that describe it much better than I can...

http://www.cse.psu.edu/~xydong/files/proceedings/DAC2010/data/1964-2006_papers/PAPERS/1988/DAC88_330.PDF http://www.xilinx.com/support/documentation/sw_manuals/ug998-vivado-intro-fpga-design-hls.pdf

eatonphil
  • 101
  • 3
  • 1
    "convert pointers to arrays of a static size" Huh? Convert to what? I don't understand what you mean. – Lundin Jul 02 '14 at 13:53
  • 2
    Would this perhaps be better asked / answered on StackOverflow? – John U Jul 02 '14 at 13:56
  • Perhaps, but it has to do with High Level Synthesis... Neither Stack nor EE has an HLS tag. Since software developers don't usually think about HLS I figured there might be a group here who understand the (common!) issue I'm having. But yes, I will post it to SO as well. – eatonphil Jul 02 '14 at 13:58
  • 1
    This could be what you're looking for; That link shows how you can pass pointers to memory of constant size. – sherrellbc Jul 02 '14 at 14:32
  • 3
    This question appears to be off-topic because it is about source code analysis and transformation. – Kaz Jul 02 '14 at 14:45
  • @Kaz, I'm not trying to do anything other than get a headstart on this issue. I'd appreciate any assistance in relocating the topic or anything else. Telling me the question is off-topic is not super helpful. – eatonphil Jul 02 '14 at 14:47
  • 1
    @phileaton I would say, the comp.compilers or comp.lang.misc Usenet newsgroups, maybe. – Kaz Jul 02 '14 at 15:38
  • None of the function arguments are arrays; they are pointers. There isn't an array declaration to be seen in the code snippet. In C, you cannot pass arrays into functions or return them from functions, unless they are wrapped in a struct,e.g. struct fixedarray { float a[32]; }. This struct can be passed and returned by value. – Kaz Jul 02 '14 at 16:35
  • I think you mean a+i, no?? In any case, the subroutine uses a pointer to a float and a float. There is no storage of the entire array in this subroutine. It doesn't need to store the whole array it acts on. I don't see a problem. – Scott Seidman Jul 02 '14 at 20:14
  • @Kaz, I think he wants to pass pointer of a constant size (at compile-time). Check out the link I posted above, there are some interesting ways to do exactly what he asks (at least my interpretation thereof). As for the synthesization referred to I am unsure. – sherrellbc Jul 02 '14 at 20:15
  • @sherrellbc et al, I updated my question with a couple of links that might illustrate what I mean by synthesis. However, I'm still at a loss for the name of what I'm actually trying to do. – eatonphil Jul 02 '14 at 20:57
  • The declaration float a[SIZE] still declares a pointer if a is a function argument. ISO C requires the declaration to satisfy constraint rules pertaining to arrays (so for instance a diagnostic is required if SIZE is present, and isn't a positive integer). After that, a becomes a "pointer to float". We see this often in main which can be declared as int main(int c, char **v) or, equivalently, int main(int c, char *v[]). – Kaz Jul 02 '14 at 21:50
  • Use a more suitable language in the first place. –  Jul 08 '14 at 10:42
  • @BrianDrummond, as much as I'd like to write FPGA logic in Verilog I think I would rather do it in C and have someone translate it for me. That is like avoiding python because you don't have access to pointers to scalar types. It's not wrong, there's just a different way to do what you want. And you use it because it's higher level than C. Same thing here. – eatonphil Jul 08 '14 at 12:03
  • I would use a higher level language than C too. But agreed you may not have the choice, C/C++ is everywhere, despite its unsuitability. –  Jul 08 '14 at 12:56

3 Answers3

1

you can declare an array of fixed size and then access it using a pointer, take for instance you can declare

float a[10];

and access it as

*(a+index)= <value>;

*a representing a[0].

for your sum function, you can leave the following function intact

`    void sum(float *a, int size, float *out) {
        int i;
        for (i=0;i<size;i++)
            *out += a+1;
    }

and make changes in the calling program preprocessor directive

#define size <yourchoice>

variable declaration

float a[size];
float out[size];

function call

sum((float*)a,int size,(float*)out);

m not sure what your function is trying to achieve block addition or sum of consecutive floats...but hope this helps

Himanshu Sourav
  • 249
  • 1
  • 2
  • 11
0

In C you don't pass arrays by value to functions, instead you just pass the pointer, ie an address of a memory location that act as a valid storage for your data. This is the reason some trick has to be used to pass the size ( ie the number of elements accessible ). This is usually called as a "by ref" parameter passing in some higher level languages. In your question you seems almost ignore the *out pointer. Indeed is important that the caller pass a pointer to a valid memory portion wide at least size*sizeof(float) bytes. Note than a +1 on a float * means a sizeof(float) increment to the absolute value of the address (usually 4 bytes). Don't know if this is the answer you want, but this is the essence of C, the fact that makes C an usually fast language.

Felice Pollano
  • 893
  • 9
  • 20
0

This is not possible in the general case.

It might be possible in your specific case, writing a tool that takes the first declaration of an array and forces that size on called functions. But what if a function is called with arrays of different or variable sizes?

In general C-to-Verilog produces poor results, especially with code that wasn't written to be retargeted. How large is the code you're trying to target, and what roughly does it do?

pjc50
  • 46,725
  • 4
  • 65
  • 126