gcc中的运行时参数(反向va_args / varargs) [英] Run-time parameters in gcc (inverse va_args/varargs)

查看:259
本文介绍了gcc中的运行时参数(反向va_args / varargs)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试对我正在开发的微控制器的解释器进行一些改进。对于执行内置函数,我目前有这样的东西(虽然有点快):

 函数executeBuiltin(functionName,functionArgs) {
if(functionName ==foo)foo(getIntFromArg(functionArgs [0]));
if(functionName ==bar)bar(getIntFromArg(functionArgs [0]),getBoolFromArg(functionArgs [1]),getFloatFromArg(functionArgs [2]));
if(functionName ==baz)baz();
...
}

但它适用于嵌入式设备(ARM )资源非常有限,我需要大幅缩减代码大小。我想要做的是拥有一个通用函数,用不同参数调用其他函数 - 如下所示:

 函数executeBuiltin(functionName,functionArgs){
functionData = fast_lookup(functionName);
call_with_args(functionData.functionPointer,functionData.functionArgumentTypes,functionArgs);
}

所以我希望能够调用一个标准的C函数并传递它它需要的参数(可能都是不同的类型)。为此,我需要一个 call_with_args 函数。

我想避免重写每一个函数来取argc + argv。理想情况下,所调用的每个函数都将是一个完全标准的C函数。



有一个关于此处的讨论 - 但自1993年发布该帖子以来,发生了什么变化?特别是当我在ARM上运行时,参数在寄存器中而不是在堆栈上。即使它不是标准C语言,有没有什么特定的GCC可以完成?




更新:看起来,尽管根据规范,行为是'未定义的',但它看起来像是因为C调用的方式,你可以将更多的参数传递给一个函数而不是期望的,并且一切都会很好,所以你可以解压缩所有参数写入一个uint32s数组中,然后可以将每个uint32传递给该函数。



这使得为调用编写'nice'代码变得更加容易,并且它出现了工作得很好(在32位平台上)。唯一的问题似乎是传递64位数字并编译为64位x86,因为它似乎在这种情况下做了特别奇怪的事情。

解决方案

在编译时可以使用宏吗?
大致如下:
https ://www.redhat.com/archives/libvir-list/2014-March/msg00730.html



如果需要运行时,可能是__buildin_apply_args()可以被利用。


I'm trying to make some improvements to a interpreter for microcontrollers that I'm working on. For executing built-in functions I currently have something like this (albeit a bit faster):

function executeBuiltin(functionName, functionArgs) {
  if (functionName=="foo") foo(getIntFromArg(functionArgs[0]));
  if (functionName=="bar") bar(getIntFromArg(functionArgs[0]),getBoolFromArg(functionArgs[1]),getFloatFromArg(functionArgs[2]));
  if (functionName=="baz") baz();
  ...
}

But it is for an embedded device (ARM) with very limited resources, and I need to cut down on the code size drastically. What I'd like to do is to have a general-purpose function for calling other functions with different arguments - something like this:

function executeBuiltin(functionName, functionArgs) {
  functionData = fast_lookup(functionName);
  call_with_args(functionData.functionPointer, functionData.functionArgumentTypes, functionArgs);
}

So I want to be able to call a standard C function and pass it whatever arguments it needs (which could all be of different types). For this, I need a call_with_args function.

I want to avoid re-writing every function to take argc+argv. Ideally each function that was called would be an entirely standard C function.

There's a discussion about this here - but has anything changed since 1993 when that post was written? Especially as I'm running on ARM where arguments are in registers rather than on the stack. Even if it's not in standard C, is there anything GCC specific that can be done?


UPDATE: It seems that despite behaviour being 'undefined' according to the spec, it looks like because of the way C calls work, you can pass more arguments to a function than it is expecting and everything will be fine, so you can unpack all the arguments into an array of uint32s, and can then just pass each uint32 to the function.

That makes writing 'nice' code for calls much easier, and it appears to work pretty well (on 32 bit platforms). The only problem seems to be when passing 64 bit numbers and compiling for 64bit x86 as it seems to do something particularly strange in that case.

解决方案

Would it be possible to do at compile time with macros? Something along the lines of: https://www.redhat.com/archives/libvir-list/2014-March/msg00730.html

If runtime was required, perhaps __buildin_apply_args() could be leveraged.

这篇关于gcc中的运行时参数(反向va_args / varargs)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆