查看: 1261|回复: 0
收起左侧

RISC-V:__global_pointer$

[复制链接]

  离线 

  • TA的每日心情
    奋斗
    2021-3-3 12:32
  • 签到天数: 10 天

    [LV.3]

    发表于 2020-8-24 10:30:20 | 显示全部楼层 |阅读模式

    有人预言,RISC-V或将是继Intel和Arm之后的第三大主流处理器体系。欢迎访问全球首家只专注于RISC-V单片机行业应用的中文网站

    您需要 登录 才可以下载或查看,没有帐号?立即注册

    x
    本帖最后由 皋陶 于 2020-8-26 18:26 编辑

    RISC-V:__global_pointer$

    原文出处:https://gnu-mcu-eclipse.github.i ... al-pointer-register


    The gp (Global Pointer) register is a solution to further optimise memory accesses within a single 4KB region.


    The linker uses the __global_pointer$ symbol definition to compare the memory addresses and, if within range, it replaces absolute/pc-relative addressing with gp-relative addressing, which makes the code more efficient. This process is also called relaxing, and can be disabled by -Wl,--no-relax.


    1. $ cat main.c
    2. int i;
    3. int main()
    4. {
    5.   return i;
    6. }

    7. $ riscv-none-embed-gcc  main.c --save-temps
    8. $ cat main.s
    9. ...
    10.         lui        a5,%hi(i)
    11.         lw        a5,%lo(i)(a5)
    12. ...
    13. $ riscv-none-embed-objdump -d a.out
    14. ...
    15.    101b4:       8341a783                lw      a5,-1996(gp) # 11fdc <i>
    16. ...
    复制代码


    The gp register should be loaded during startup with the address of the __global_pointer$ symbol and should not be changed later.


    1. .section .reset_entry,"ax",@progbits
    2.         .align        1
    3.         .globl        _reset_entry
    4.         .type        _reset_entry, @function
    5. _reset_entry:

    6. .option push
    7. .option norelax
    8.         la gp, __global_pointer$
    9. .option pop

    10.         la sp, __stack

    11.         j _start
    复制代码


    The 4K region can be anywhere in the addressed memory, but, for the optimisation to be effective, it should preferably cover the most intensely used RAM area. For standard newlib applications, this is the area where the .sdata section is allocated, since it includes variables like _impure_ptr, __malloc_sbrk_base, etc. Thus, the definition should be placed right before the .sdata section. For example:


    1. PROVIDE( __global_pointer$ = . + (4K / 2) );
    2. *(.sdata .sdata.*)
    复制代码


    The region size is 4K because RISC-V immediate values are 12-bit signed values, which are +/- 2048 in decimal or +/- 0x800 in hex; since the values are signed, the __global_pointer$ must point to the middle of the region.


    注:要让 relaxing 优化起作用,编译时要加入 -msmall-data-limit=n 参数,有了这个参数,编译器会把内存空间小于 n 字节的静态变量放入 .sdata 或者 .sdata.* 节,然后链接器将这部分静态变量集中在 __global_pointer$ +/- 2K 的范围内。



    本篇完,感谢关注:RISC-V单片机中文网




    上一篇:一文看懂RISC-V:异构IoT时代全新架构
    下一篇:Intel x86/ARM对市场的统治会被RISC-V打破吗?
    RISCV作者优文
    全球首家只专注于RISC-V单片机行业应用的中文网站
    回复

    使用道具 举报

    高级模式
    B Color Image Link Quote Code Smilies

    本版积分规则

    关闭

    RISC-V单片机中文网上一条 /2 下一条



    版权及免责声明|RISC-V单片机中文网 |网站地图

    GMT+8, 2024-4-20 12:34 , Processed in 0.682961 second(s), 45 queries .

    快速回复 返回顶部 返回列表