Files
autosar_standard_spec_v4.4/BSWGeneral/AUTOSAR_SWS_CompilerAbstraction.md
T

273 lines
8.5 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# 编译器抽象规范
> **AUTOSAR CP Release 4.4.0**
>
> 原文:*Specification of Compiler Abstraction*(文档 ID 051
>
> 翻译状态:**已完成 v1**
>
> 对应原文 PDF`BSWGeneral/AUTOSAR_SWS_CompilerAbstraction.pdf`
---
## 文档标识
| 字段 | 值 |
|------|-----|
| 文档标题 | 编译器抽象规范(Specification of Compiler Abstraction |
| 文档标识号 | 051 |
| 文档状态 | 正式版(Final) |
| 所属标准 | Classic Platform |
| 所属版本 | 4.4.0 |
---
## 文档变更历史(节选)
| 日期 | 版本 | 变更说明 |
|------|------|----------|
| 2018-10-31 | 4.4.0 | 编辑性修订 |
| 2017-12-08 | 4.3.1 | 编辑性修订;澄清模块特定内存类和全局内存类 |
| 2016-11-30 | 4.3.0 | 移除 Variants 章节;移除过时元素 |
| 2015-07-31 | 4.2.2 | 清理需求追踪;澄清编译器符号列表 |
| 2014-10-31 | 4.2.1 | 编译器符号定义不允许包含值;重构文档结构;移除 MISRA/C/C++ 引用;修正引用 |
| 2013-03-15 | 4.1.1 | 新增 `CONSTP2FUNC` 抽象宏(指向函数的常量指针);改进与 Memory Mapping 的一致性 |
| 2011-12-22 | 4.0.3 | 新增 `FUNC_P2CONST``FUNC_P2VAR` 宏;新增 `REGSPACE` 指针类(用于寄存器访问) |
| 2010-02-02 | 3.1.4 | 编译器抽象扩展以适用于软件组件;移除 `STATIC` 声明关键字;新增 `LOCAL_INLINE` 关键字(用于 `static inline` 函数实现) |
---
## 1 介绍与功能概述
本文档规定 **AUTOSAR 编译器抽象**。编译器抽象的目标是为 AUTOSAR 软件组件和 BSW 模块提供**独立于具体编译器**的关键字、宏和类型定义。
通过使用 `Compiler.h` 中定义的抽象宏(如 `FUNC``P2VAR``CONST` 等),代码可在不同编译器间移植。
**重要**:本文档**只规范抽象宏的定义**,**不**规范宏的**值**(值由编译器厂商在 `Compiler_Cfg.h` 中提供)。
---
## 2 相关文档
| 编号 | 名称 | 文件 |
|------|------|------|
| [1] | General Requirements on Basic Software Modules | `AUTOSAR_SRS_BSWGeneral.pdf` |
| [2] | General Specification of Basic Software Modules | `AUTOSAR_SWS_BSWGeneral.pdf` |
| [3] | Specification of Platform Types | `AUTOSAR_SWS_PlatformTypes.pdf` |
| [4] | Specification of Standard Types | `AUTOSAR_SWS_StandardTypes.pdf` |
| [5] | Specification of Memory Mapping | `AUTOSAR_SWS_MemoryMapping.pdf` |
| [6] | ISO/IEC 9899:1990 Programming Language C | — |
| [7] | ISO/IEC 14882:2003 Programming Language C++ | — |
---
## 3 约束与假设
### 3.1 限制
无。
### 3.2 对车辆域的适用性
适用于所有车辆域。
---
## 4 对其他模块的依赖
| 模块 | 说明 |
|------|------|
| `Platform_Types.h` | 提供 `boolean``uint8` 等基础类型 |
| `Compiler_Cfg.h` | 编译器特定配置(由编译器厂商提供) |
---
## 5 需求追踪
> 约 30 项 SRS_BSW_* 需求追踪。完整列表参见英文原版 PDF 第 13-17 页。
---
## 6 文件结构
### 6.1 代码文件结构
本模块是**纯头文件**模块,不提供 `.c` 文件。
### 6.2 头文件结构
```
Compiler.h (本文档规范)
├── 关键字(关键字抽象)
│ ├── FUNC(rettype, memclass) 函数定义
│ ├── FUNC_P2CONST(rettype, ...) 返回 const 指针的函数
│ ├── FUNC_P2VAR(rettype, ...) 返回 var 指针的函数
│ ├── CONSTP2CONST(type, memclass, ptrclass) 指向 const 的 const 指针
│ ├── CONSTP2VAR(type, memclass, ptrclass) 指向 var 的 const 指针
│ ├── CONSTP2FUNC(type, memclass, ptrclass) 指向函数的 const 指针
│ ├── P2CONST(type, memclass, ptrclass) 指向 const 的指针
│ ├── P2VAR(type, memclass, ptrclass) 指向 var 的指针
│ ├── P2FUNC(type, memclass, ptrclass) 指向函数的指针
│ ├── CONST(type, memclass) const 限定符
│ ├── VAR(type, memclass) var 限定符
│ ├── LOCAL_INLINE static inline 关键字
│ └── _ (关键字本身由编译器实现)
└── 类型
└── NULL_PTR 空指针常量
```
---
## 7 API 规范
### 7.1 关键字
#### 7.1.1 `FUNC`
> **[SWS_COMPILER_00046]** `FUNC` 宏用于**函数定义**的抽象:
> ```c
> #define FUNC(rettype, memclass) rettype
> ```
>
> 编译器在 `Compiler_Cfg.h` 中按 `memclass` 映射到具体的函数修饰符(如 `static`、`__interrupt` 等)。
#### 7.1.2 `FUNC_P2CONST` / `FUNC_P2VAR`
> **[SWS_COMPILER_00060]** `FUNC_P2CONST` 宏用于**返回 const 指针的函数**定义:
> ```c
> #define FUNC_P2CONST(rettype, ptrclass, memclass) rettype
> ```
>
> **[SWS_COMPILER_00061]** `FUNC_P2VAR` 宏用于**返回 var 指针的函数**定义:
> ```c
> #define FUNC_P2VAR(rettype, ptrclass, memclass) rettype
> ```
#### 7.1.3 `P2CONST` / `P2VAR` / `P2FUNC`
> **[SWS_COMPILER_00058]** `P2CONST` 宏用于**指向 const 数据的指针**:
> ```c
> #define P2CONST(ptrtype, memclass, ptrclass) const ptrtype *
> ```
>
> **[SWS_COMPILER_00059]** `P2VAR` 宏用于**指向 var 数据的指针**:
> ```c
> #define P2VAR(ptrtype, memclass, ptrclass) ptrtype *
> ```
>
> **[SWS_COMPILER_00057]** `P2FUNC` 宏用于**指向函数的指针**:
> ```c
> #define P2FUNC(ptrtype, memclass, ptrclass) ptrtype (*)
> ```
#### 7.1.4 `CONSTP2CONST` / `CONSTP2VAR` / `CONSTP2FUNC`
> **[SWS_COMPILER_00062]** `CONSTP2CONST`:指向 const 数据的 **const 指针**
> ```c
> #define CONSTP2CONST(ptrtype, memclass, ptrclass) const ptrtype * const
> ```
>
> **[SWS_COMPILER_00063]** `CONSTP2VAR`:指向 var 数据的 **const 指针**
> ```c
> #define CONSTP2VAR(ptrtype, memclass, ptrclass) ptrtype * const
> ```
>
> **[SWS_COMPILER_00064]** `CONSTP2FUNC`:指向函数的 **const 指针**
> ```c
> #define CONSTP2FUNC(ptrtype, memclass, ptrclass) ptrtype (* const)
> ```
#### 7.1.5 `CONST` / `VAR`
> **[SWS_COMPILER_00065]** `CONST` 宏用于**只读**类型限定:
> ```c
> #define CONST(type, memclass) const type
> ```
>
> **[SWS_COMPILER_00066]** `VAR` 宏用于**可读可写**类型限定:
> ```c
> #define VAR(type, memclass) type
> ```
#### 7.1.6 `LOCAL_INLINE`
> **[SWS_COMPILER_00067]** `LOCAL_INLINE` 宏替代 `static inline` 关键字(自 R3.1.4 起):
> ```c
> #define LOCAL_INLINE static inline
> ```
#### 7.1.7 `NULL_PTR`
> **[SWS_COMPILER_00039]** 空指针常量:
> ```c
> #define NULL_PTR ((void *)0)
> ```
### 7.2 类型
无(不定义新类型,仅使用 `Platform_Types.h` 中定义的基础类型)。
---
## 8 配置规范
`Compiler_Cfg.h` 由**编译器厂商**提供,包含所有抽象宏到具体编译器关键字的映射。
**示例**(用于某 32 位 MCU 的 C 编译器):
```c
/* Compiler_Cfg.h */
#define AUTOMATIC
#define TYPEDEF
#define FUNC(rettype, memclass) rettype
#define P2VAR(ptrtype, memclass, ptrclass) ptrtype *
#define CONST(consttype, memclass) const consttype
#define VAR(vartype, memclass) vartype
```
---
## 9 序列图
不适用。
---
## 10 不适用的需求
> **[SWS_COMPILER_00999]** 这些需求**不适用于**本规范。
>
> 不适用的 SRS_BSW 需求包括调度、资源使用、错误处理、模块初始化等。
---
## 11 关键字使用示例
```c
/* 示例:使用编译器抽象的函数声明 */
#define CAN_START_SEC_CODE
#include "Can_MemMap.h" /* 来自 Memory Mapping 规范 */
FUNC(void, CAN_CODE) Can_Write(
P2VAR(Can_HwHandleType, AUTOMATIC, APPL_DATA) Hth,
P2CONST(PduInfoType, AUTOMATIC, CAN_APPL_DATA) PduInfo
);
#define CAN_STOP_SEC_CODE
#include "Can_MemMap.h"
```
其中:
- `FUNC(void, CAN_CODE)` 定义返回 `void`、存储类为 `CAN_CODE` 的函数
- `P2VAR(..., AUTOMATIC, APPL_DATA)` 定义指向 `Can_HwHandleType` 的指针(自动存储、APPL_DATA 指针类)
- `P2CONST(..., AUTOMATIC, CAN_APPL_DATA)` 定义指向 `PduInfoType` 的 const 指针
---
## 翻译说明
- 本文档为**编译器抽象宏规范**,核心是 C 宏定义
- 所有宏名(`FUNC``P2VAR``CONST` 等)保持英文
- 内存类(`CAN_CODE`)、指针类(`APPL_DATA``CAN_APPL_DATA`)由具体模块在 `Compiler_Cfg.h` 中定义
- 编译器抽象与 Memory Mapping 紧密相关(见 `AUTOSAR_SWS_MemoryMapping.pdf`
---
*翻译:opencode-translator / Step 3 P0 批量翻译*