Quantcast
Channel: Question and Answer » asm
Viewing all articles
Browse latest Browse all 6

Implementing non-fixed length array support in a compiler

$
0
0

I’m thinking of building a language for PIC microcontrollers. I want to be able to use non-fixed size arrays, like this:

  1. Declare the variable as int[]
  2. Wait for input from serial connection
  3. Make the variable input long

I think such a feature would be useful, but don’t know how I should go about implementing this in a compiler which compiles to assembly. I want the arrays to be stored on consecutive register addresses, of course. Since I’m working with PICs, this has to be very memory efficient.

When someone writes int[], I don’t think it’s a good idea to reserve memory space for the variable yet, is it? Then the array would have a fixed maximum size. For example, when the array gets reserved memory address 5-100, and other variables get 4 and 101, the array has fixed borders and can’t grow any bigger than 96 registers. Also, when I’d reserve memory from the start, let’s say x bytes, and I in the end only need y bytes, I’m wasting xy bytes. I don’t want that.

That means the only option I see is initializing the array and reserving space in the microcontroller, on the fly. This will take up some memory and execution time, of course. I thought of a system like this:

  • Initialize an array int[x] = {int, int} which holds pointers to the start and end of arrays that are not initialized from the beginning – x would be the maximum amount of arrays (this is a concession, but is better than a maximum length for all arrays)
  • Store a variable c = 0 to indicate the number of arrays used
  • Store the borders of the initialized (reserved) memory in a variable somewhere
  • When an array gets a length:

    • Put pointers to the start (the current border) and end (the current border + the length) in the array from the first point at index c
    • Increment c

I think this would work (wouldn’t it?), but there a few cons, mainly concerning memory: I need to store the array, c and the current memory borders as overhead.

Would there be a better way to implement non-fixed size arrays in a language for PIC microcontrollers? My requirements are:

  • Low memory overhead
  • The array length does not have to be changed on the fly
  • With the system I thought up, one cannot store values in arrays that haven’t been initialized yet. If there would be a system that can store values in an array of undefined length, that would be an advantage
  • Faster systems (at runtime, compile time doesn’t matter) are preferable

Viewing all articles
Browse latest Browse all 6

Trending Articles