Code Monkey home page Code Monkey logo

Comments (2)

lujji avatar lujji commented on July 23, 2024

Hi Georg, glad you found it useful.

Usually, when executing from RAM the biggest problem with function calls is relative addressing. For example: you have functions f_ram() and f_flash(), where f_ram() is copied into RAM during start-up. Let's say at some point your RAM function calls a function from flash (and compiler knows nothing about our intentions to put f_ram() somewhere else). Since both functions reside in the same memory section, the compiler might use more compact relative addressing: instead of calling f_flash() by its absolute address (i.e. setting PC = f_flash_address) an offset will be used (PC = PC + some_offset). Now if we copy the resulting code into RAM, this will no longer work, since program counter is in a different place and some_offset will point to a wrong location.

I'm not sure if this explanation was necessary, but I just wanted to make sure that I understood your question correctly and that we're talking about the same thing :)

Now the thing about SDCC is that it always uses call/ret instructions regardless of the position. In other words, you're always using absolute calls, so the above example will work properly.

The situation is different with jumps though: a relative jump instruction takes a signed 8-bit offset, which won't take you far, so if your function is large enough, absolute jumps will be used. That will be an issue when executing from RAM, as you've already mentioned. Forcing the compiler to use relative jumps only and adding veneers when necessary would be a workaround, although I believe it's not possible with SDCC. Another workaround is the one you suggested: linking required sections in RAM and processing compiled objects with external tools. To be honest, I don't think any of these approaches are correct - RAM code should be handled by the compiler/linker to begin with. Perhaps Philipp [@spth] might give us some insight? The example in question is this one.

from stm8-bare-min.

spth avatar spth commented on July 23, 2024

I'm not an expert on linker stuff. Code generation emits absolute jumps / calls only. Then the peephole optimizer transforms them into relative ones where it can (this only happens for calls / jumps within the same function, as the peephole optimizer works function by function).

The linker fills in the absolute addresses for global / static variables, absolute jumps and calls. If you have a function that will only be executed from RAM, we'd need the linker to fill in the addresses as if the function was already there. Maybe specifiying a different code segment for that function (i.e. place it in a different source file and use --codeseg when compiling that file will help; if you want it in the same file as the other functions, using #pragma codeseg should do). Then use linker options, maybe -b or -g to tell the linker about the location of the function in RAM.

Philipp

from stm8-bare-min.

Related Issues (6)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.